【问题标题】:use CLLocation to get an address book contact Xcode使用 CLLocation 获取地址簿联系人 Xcode
【发布时间】:2017-02-28 14:12:32
【问题描述】:

我有一个应用程序,可以在其中获取相机胶卷中图像的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);
    }

【问题讨论】:

    标签: ios xcode contacts cllocation alasset


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      所以我想通了

      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) {
      
          }
      

      感谢大家的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 1970-01-01
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多