How to get street address from ABPeoplePickerNavigationController

I need a contact address. I know how to get the properties of a single value, but the street address is a multi-valued property. Apple documentation shows how to install, but not remove. Any help?

PS: this does not work:

ABRecordCopyValue(person, kABPersonAddressStreetKey); 
+4
source share
6 answers

I just realized:

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

Quick version:

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

You can access a separate address field by providing the appropriate key:

 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] 
+3
source

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" } 
+2
source

If the user has several specified addresses - work, home, etc., you will need to use the identifier attribute to distinguish between them. What I get is selected from similar messages to email addresses:

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

.

+1
source

I believe that it should work like this (obtained from the documentation, not verified):

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

The address book user interface structure is deprecated in iOS 9. Instead, use the APIs defined in the ContactsUI structure. To learn more, see ContactsUI .

0
source

Source: https://habr.com/ru/post/1383916/


All Articles