Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a list of all the properties of an ABRecordRef?

I would like to obtain a list of all the properties of an ABPersonRef and ABGroupRef without having to use the iOS predefined keys of kABPersonFirstNameProperty, kABPersonLastNameProperty... I'm playing with the address book and I'd like to iterate over all values for a particular person. I know there are predefined keys but Apple could very well add new ones in the future so I'd like to do something like:

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < [allPeople count]; ++i) {
    ABRecordRef person = [allPeople objectAtIndex:i];

    // This is the line that I can't figure out.
    NSArray *allProperties = (NSArray *)ABRecordCopyArrayOfAllProperties(person);
}

I know that I'll encounter multivalue items that I'll have to loop though later, but the goal is to obtain a list of keys that I can iterate over for the single value properties. I don't care what the returned class is, NSArray, NSDictionary... whatever.

I greatly appreciate any advice!

like image 848
Dave Avatar asked Aug 03 '11 17:08

Dave


1 Answers

You can try the following:

With ARC:

NSDictionary* dictionaryRepresentationForABPerson(ABRecordRef person)
{
    NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];

    for ( int32_t propertyIndex = kABPersonFirstNameProperty; propertyIndex <= kABPersonSocialProfileProperty; propertyIndex ++ )
    {
        NSString* propertyName = CFBridgingRelease(ABPersonCopyLocalizedPropertyName(propertyIndex));
        id value = CFBridgingRelease(ABRecordCopyValue(person, propertyIndex));

        if ( value )
            [dictionary setObject:value forKey:propertyName];
    }

    return dictionary;
}
  • We using the localized name of the property - in different locales will have different keys.
  • The number of properties may change in the next version of iOS.

Maybe it makes sense to go through to the number of properties as long as the propertyName does not become UNKNOWN_PROPERTY

like image 92
Aliaksandr Andrashuk Avatar answered Oct 06 '22 07:10

Aliaksandr Andrashuk