Question

This little gem cost me a couple of hours of debugging so I thought I'd post the problem (and solution) here. I'm creating an Array of address book constants (kABPerson*Property) that I later use for extracting contact data. Surprisingly, the values all appear to be zero. It looks like the backing for these are actually global variables even though they look like constants (on iOS 7 anyway), but I haven't been able to track down any memory overwrite problems. What the heck could be going on?

Was it helpful?

Solution

It turns out that the kABPerson*Property "constants" are actually initialized on the first call to ABAddressBookCreate. Before that the values are all zero. Here is some sample code:

    NSLog(@"Before AddressBook Create, kABPersonLastNameProperty = %d", kABPersonLastNameProperty);
    ABAddressBookRef store = ABAddressBookCreateWithOptions(NULL, NULL);
    NSLog(@"After AddressBook Create, kABPersonLastNameProperty = %d", kABPersonLastNameProperty);

If you haven't accessed the address book before, you would see output like:

    Before AddressBook Create, kABPersonLastNameProperty = 0
    After AddressBook Create, kABPersonLastNameProperty = 1

I Other calls may initialize the constants as well but from my exploration a call to ABAddressBookGetAuthorizationStatus is not sufficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top