My method of programmatically retrieving e-mail addresses from the Address Book no longer seems to work on iOS 6 devices. It worked in iOS 5 and oddly, still works in the iOS 6 Simulator. Is there a new way to programmatically retrieve contacts from a users' Address Book?
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
self.contacts = [[NSMutableArray alloc] init];
int contactIndex = 0;
for (int i = 0; i < nPeople; i++) {
// Get the next address book record.
ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);
// Get array of email addresses from address book record.
ABMultiValueRef emailMultiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
NSArray *emailArray = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue);
[self.contacts addObject:emailArray];
}
To clarify, the above does not crash, it simply returns no results. ABAddressBookCopyArrayOfAllPeople is empty. Thanks!
Probably related to the new privacy controls—as of iOS 6, on the device, an app can’t access the user’s contacts without their permission. From the documentation:
If you haven’t seen the permissions alert come up (“SomeApp would like access to your contacts”), it’s possible that the direct address-book APIs just assume that they don’t have access and silently fail; you might have to display something from the AddressBookUI framework to trigger it.
I created a helper class,
AddressBookHelper
, to handle backward compatibility. Here are the guts:Probably related to the new privacy controls, as of iOS 6, on the device, an app can’t access the user’s contacts without their permission.
Code:
Try with this: access to the address book must be granted before it can be access programmatically. Here is what I ended up doing.