How to get Country field from ABAddressBookRef?

I am having trouble understanding how to access the address properties in ABAddressBookRef. I did it fine with phone numbers:

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty); NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty); CFRelease(phoneNumberProperty); 

But alas ... I can’t figure out how to do this for addresses. If I do this:

 ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty); NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty); 

I am returning what looks like a dictionary, but typed as an array. How to access properties inside it? I saw many different sentences on the Internet, but they all seem to contain about 30 lines of code, just to get one line out of a dictionary!

Can anyone help? Thanks!

+4
source share
1 answer

For addresses, you get an array of dictionaries so that you loop through the array and extract the key value from each dictionary:

 ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty); NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty); for (NSDictionary *addressDict in address) { NSString *country = [addressDict objectForKey:@"Country"]; } CFRelease(addressProperty); 

You can also loop directly through ABMultiValueRef instead of first converting it to NSArray:

 ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty); for (CFIndex i = 0; i < ABMultiValueGetCount(addressProperty); i++) { CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressProperty, i); NSString *country = (NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey); CFRelease(dict); } CFRelease(addressProperty); 
+11
source

All Articles