0

I'm trying to get the Country from a ABPerson object in Cocoa.

What I do is:

NSString *country = [person valueForProperty:kABAddressCountryKey];

I get this in the console:

-[ABPerson valueForProperty:Country] - unknown property. This warning will be displayed only once per unknown property, per session.

Getting the persons' organizationName(kABOrganizationProperty), firstName (kABFirstNameProperty) and lastName (kABLastNameProperty) works.

Any ideas?

Mikael
  • 3,572
  • 1
  • 30
  • 43

1 Answers1

0

As the key kABAddressCountryKey implies it represents a value of an address. As person can have multiple addresses (represented as dictionaries) you have to loop the addresses to get the country:

ABMultiValueRef addressesRef = ABRecordCopyValue(personRef, kABPersonAddressProperty);

for (int i = 0; i < ABMultiValueGetCount(addressesRef); i++)
{
    CFDictionaryRef oneAddressRef = ABMultiValueCopyValueAtIndex(addressesRef, i);
    NSString *country = CFRetain(CFDictionaryGetValue(oneAddressRef, kABPersonAddressCountryCodeKey));
    // Do fancy things with country...
    CFRelease(country);
    CFRelease(oneAddressRef);
}

CFRelease(addressesRef);

Haven't finally tested it, but it should work that way. Also consider the Core Foundation Memory Management.

Florian Mielke
  • 3,310
  • 27
  • 31
  • Thanks for pushing me in the right direction. I had a look at Apples documentation here as well: https://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AddressBook/Tasks/AccessingData.html#//apple_ref/doc/uid/20001023-CJBDFIGI – Mikael Aug 06 '13 at 13:23