如何从ABPeoplePickerNavigationController获取街道地址

我需要一个联系人的街道地址。 我知道如何获得单值属性,但街道地址是一个多值属性。 苹果的文档显示如何设置,但不能检索它。 任何帮助?

PS:这不起作用:

ABRecordCopyValue(person, kABPersonAddressStreetKey); 

我只是想通了:

 ABMultiValueRef st = ABRecordCopyValue(person, kABPersonAddressProperty); if (ABMultiValueGetCount(st) > 0) { CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(st, 0); self.street.text = CFDictionaryGetValue(dict, kABPersonAddressStreetKey); } 

Swift版本:

  if let addresses : ABMultiValueRef = ABRecordCopyValue(person, kABPersonAddressProperty)?.takeRetainedValue() as ABMultiValueRef? where ABMultiValueGetCount(addresses) > 0 { for index in 0..<ABMultiValueGetCount(addresses){ if let address = ABMultiValueCopyValueAtIndex(addresses, index)?.takeRetainedValue() as? [String:String], label = ABMultiValueCopyLabelAtIndex(addresses, index)?.takeRetainedValue() as? String{ print("\(label): \(address) \n") } } } 

您可以通过提供相应的密钥来访问个人地址字段:

 let street = address[kABPersonAddressStreetKey as String] let city = address[kABPersonAddressCityKey as String] let state = address[kABPersonAddressStateKey as String] let zip = address[kABPersonAddressZIPKey as String] let country = address[kABPersonAddressCountryKey as String] let code = address[kABPersonAddressCountryCodeKey as String] 

Swift 3.0

 //Extract billing address from ABRecord format and assign accordingly let addressProperty: ABMultiValue = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValue if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary { print(dict[String(kABPersonAddressStreetKey)] as? String) print(dict[String(kABPersonAddressCityKey)] as? String) print(dict[String(kABPersonAddressStateKey)] as? String) print(dict[String(kABPersonAddressZIPKey)] as? String) print(dict[String(kABPersonAddressCountryKey)] as? String) //"United States" } 

如果用户有多个地址定义 – 工作,家庭等,您将需要使用标识符属性来区分它们。 我从电子邮件地址的类似post中挑选出来的是:

 #pragma mark - ABPeoplePickerNavigationControllerDelegate - (IBAction)chooseContact:(id)sender { ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentViewController:picker animated:YES completion:nil]; // [self dismissViewControllerAnimated:YES completion:nil]; } - (void) peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [self dismissViewControllerAnimated:YES completion:nil]; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { return YES; } - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { if (property == kABPersonAddressProperty) { ABMultiValueRef addresses = ABRecordCopyValue(person, property); CFIndex addressIndex = ABMultiValueGetIndexForIdentifier(addresses, identifier); CFDictionaryRef address = ABMultiValueCopyValueAtIndex(addresses, addressIndex); // create address string to lookup NSString *street = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStreetKey); NSString *city = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCityKey); NSString *state = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStateKey); NSString *postal = (NSString*) CFDictionaryGetValue(address, kABPersonAddressZIPKey); NSString *country = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryKey); CFRelease(address); CFRelease(addresses); [self dismissViewControllerAnimated:YES completion:nil]; return NO; } return YES; } 

我想它应该像这样工作(从文档派生,未经testing):

 ABMultiValueRef addressMultiValue = ABRecordCopyValue(person, kABPersonAddressProperty); CFArrayRef allAddresses = ABMultiValueCopyArrayOfAllValues(addressMultiValue); CFDictionaryRef firstAddress = CFArrayGetValueAtIndex(allAddresses, 0); CFStringRef street = CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey); NSLog(@"%@", (__bridge NSString *)street); CFRelease(allAddresses); CFRelease(addressMultiValue);