I am trying to access Contacts/AddressBook programmatically in my application on iOS 8.
This is my code :
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
//populate
}
});
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
//populate
}
else {
// Alert telling user to change privacy setting
}
This code works absolutely fine on ios 7. But on ios 8 it throws me out of the app and then I need to re-run the app on simulator, then this works fine. Is this the normal behavior of apps trying to access Contacts or AddressBook on iOS 8.
Please guide me how to fix this issue.
I ran into the same problem after the iOS 8 update. The problem is that the Address Book Framework changed in iOS 8 and you now should use AddessBookUI and use ABPeoplePickerNavigationController and his delegate to access to the address book.
If you need to access to the contacts programatically the way I found to do that is like this
ABPeoplePickerNavigationController* peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
[self presentViewController:peoplePicker animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
ABAddressBookRef addressBook = [peoplePicker addressBook];
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
I don't know if it's the right way to do it, but is one way that works.
Hope this helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With