2016-10-19 3 views
0

J'ai une application dans laquelle j'obtiens la latitude CLLocation et la longitude d'une image dans la pellicule. Je peux obtenir l'adresse de ces coordonnées. Est-il possible de comparer cette adresse aux adresses de mes contacts et s'il y a correspondance, créez un NSString avec le nom correspondant à cette adresse de contact.utiliser CLLocation pour obtenir un contact de carnet d'adresses Xcode

// d'abord obtenir le 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); 

// alors trouver l'adresse

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); 
} 
}]; 

donc est-il un moyen de comparer cette adresse avec ceux dans les contacts des utilisateurs pour voir si est un match , puis créer un NSString avec le nom de ce contact

J'ai donc été en mesure d'accéder au carnet d'adresses et d'obtenir le prénom et nom et l'indice dans lequel ils résident.

J'ai également été en mesure de trouver l'adresse entrée pour chaque contact, mais il est sous une forme que je ne peux pas utiliser comme je dois le faire afin que je puisse comparer l'emplacement de l'image dans carnet d'adresses.

ci-dessous est mon code. Quelqu'un peut-il me aider à comprendre comment obtenir l'adresse de chaque contact afin que je puisse soit comparer l'adresse de la rue et zip code ou convertir l'adresse à un ensemble de coordonnées qui pourrait être comparé

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

Répondre

0

Je compris cela

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]; 

// Je reçois l'emplacement des actifs

  CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation]; 

// je romps que dans les composants

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

// j'inverse endroit pour obtenir une adresse de rue et le code postal et faire une chaîne à partir de ceux-ci

  [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); 

// Puis j'accède au carnet d'adresses de l'utilisateur et regarde pour l'adresse de rue et les codes postaux de chaque personne et mettez-les ensemble dans une chaîne.

       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); 

// Ensuite, je compare la chaîne d'emplacement à la chaîne d'adresse. S'il y a correspondance, j'enregistre le nom de la personne de cette adresse et je l'attache à l'adresse plus tard pour remplir une vue de texte.

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

    } 

Merci pour l'aide de tout le monde.