使用 CLLocation 获取地址簿联系人 Xcode

use CLLocation to get an address book contact Xcode

我有一个应用程序,可以在其中获取相机胶卷中图像的 CLLocation 纬度和经度。我可以获得那些坐标的地址。有什么方法可以将该地址与我的联系人地址进行比较,如果匹配,则创建一个 NSString,其名称与该联系地址相对应。

//先获取CLLocation

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
            if (asset == nil) return;
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
            NSLog(@"lat;%f",location.coordinate.latitude);
            NSLog(@"long;%f",location.coordinate.longitude);

//然后找到地址

CLGeocoder *ceo = [[CLGeocoder alloc]init];
            CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; //insert your coordinates

            [ceo reverseGeocodeLocation:loc
                      completionHandler:^(NSArray *placemarks, NSError *error) {
                          CLPlacemark *placemark = [placemarks objectAtIndex:0];
                          if (placemark) {

                              NSLog(@"placemark %@",placemark);
                              //String to hold address
                              NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                              NSLog(@"addressDictionary %@", placemark.addressDictionary);

                              NSLog(@"placemark %@",placemark.region);
                              NSLog(@"placemark %@",placemark.country);  // Give Country Name
                              NSLog(@"placemark %@",placemark.locality); // Extract the city name
                              NSLog(@"location %@",placemark.name);
                              NSLog(@"location %@",placemark.ocean);
                              NSLog(@"location %@",placemark.postalCode);
                              NSLog(@"location %@",placemark.subLocality);

                              NSLog(@"location %@",placemark.location);
                              //Print the location to console
                              NSLog(@"I am currently at %@",locatedAt);
}
}];

有没有办法将此地址与用户联系人中的地址进行比较,看看是否匹配 然后用那个联系人的名字创建一个 NSString

所以我已经能够访问地址簿并获取名字和姓氏以及他们所在的索引。

我也找到了为每个联系人输入的地址,但是它的格式我无法使用,因为我需要制作它以便我可以将图片的位置与地址簿。

下面是我的代码。谁能帮我弄清楚如何获取每个联系人的地址,这样我就可以 比较街道地址和邮政编码或 将地址转换成一组可以比较的坐标

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                // First time access has been granted, add the contact
                NSLog(@"granted");
            } else {
                // User denied access
                // Display an alert telling user the contact could not be added
                NSLog(@"denied");
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        NSLog(@"authorized");
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }

    CFErrorRef *error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    //ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

    for(int i = 0; i < numberOfPeople; i++) {
        NSLog(@"i;%d",i);

         NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];


        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );


        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
         NSLog(@"Name:%@ %@", firstName, lastName);

        ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));

        if(ABMultiValueGetCount(address) > 0) {
            [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(address, 0) forKey:@"City"];

            NSString *addressste = [dOfPerson valueForKey:@"City"];

        NSLog(@"dOfPerson;%@",[dOfPerson description]);
            NSLog(@"streetadr:%@",addressste);

        }

        NSLog(@"address:%@",address);
    }

从通讯录中获取联系人并将您的地址与联系人的街道地址进行比较,要从通讯录中获取街道地址本教程可能对您有所帮助

https://www.appcoda.com/ios-programming-import-contact-address-book/

所以我想通了

if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {

    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;

[self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
    }  


- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
    @try {
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
            if (asset == nil) return;
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];

//我得到资产位置

            CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];

//我把它分解成组成部分

      CLGeocoder *ceo = [[CLGeocoder alloc]init];
            CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude]; //insert your coordinates

//然后我反转位置以获取街道地址和邮政编码并从中创建一个字符串

            [ceo reverseGeocodeLocation:loc
                      completionHandler:^(NSArray *placemarks, NSError *error) {
                          CLPlacemark *placemark = [placemarks objectAtIndex:0];
                          if (placemark) {
                              NSLog(@"placemark %@",placemark);
                             NSString *strString =placemark.name;
                              NSString *midstring = [strString stringByAppendingString:@","];
                              if (placemark.postalCode == nil) {
                                  postalcodeString = @"00000";
                              }
                              else{
                                  postalcodeString = placemark.postalCode;
                              }

                              combineString = [midstring stringByAppendingString:postalcodeString];
                              NSLog(@"combineString:%@",combineString);

//然后我访问用户的地址簿并查找每个人的街道地址和邮政编码并将它们组合成一个字符串。

                              CFErrorRef *error = NULL;
                              ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
                              //ABAddressBookRef addressBook = ABAddressBookCreate();
                              CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
                              CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

                              for(int i = 0; i < numberOfPeople; i++) {
                                  NSLog(@"i;%d",i);

                                  ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );


                                  NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

                                  NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
                                  NSLog(@"Name:%@ %@", firstName, lastName);

                                  ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));

                                  if(ABMultiValueGetCount(address) > 0) {
                                      CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(address, 0);
                                      NSString *streetString = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
                                      NSString *zipString = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);
                                      if (zipString == Nil) {
                                          zipString = @"00000";
                                      }
                                      NSString *interstring = [streetString stringByAppendingString:@","];
                                      NSString *allString = [interstring stringByAppendingString:zipString];
                                      NSLog(@"allstring;%@ %@",allString, combineString);

//然后我将位置字符串与地址字符串进行比较。如果匹配,我会保存该地址的人名,稍后将其附加到地址以填写文本视图。

                                      if ([combineString isEqualToString:allString]) {
                                          NSLog(@"its a match");
                                          NSLog(@"i:%d",i);
                                          firstNameString = firstName;

                                      }

                                      else {

                                      }


                                      NSLog(@"streetString:%@",streetString);
                                      NSLog(@"firstNameString:%@",firstNameString);
                                      NSLog(@"combineString:%@",combineString);
                                                                      }
                              }}
                          else {
                              NSLog(@"Could not locate");
                          }
                      }
             ];
            // Do what you need with the file name here
        };

        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {

        };

        ALAssetsLibrary *library = [ALAssetsLibrary new];
        [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    }
    @catch (NSException *exception) {

    }

感谢大家的帮助。