Ios 8: Accessing Contacts / Address Book Error

I am trying to access Contacts / AddressBook programmatically in my app 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 fine on ios 7. But on ios 8 it kicks me out of the application and then I need to restart the application on the simulator then it works fine. This is a common behavior for apps trying to access contacts or address book on iOS 8.

Read how to fix this problem.

+3


source to share


1 answer


I faced the same issue after iOS 8 update. The problem is that the structure of the address book changed in iOS 8 and now you have to use AddessBookUI and use ABPeoplePickerNavigationController and its delegate to access the address book.

If you need to access contacts programmatically, then the way I found it looks 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 this is the right way to do it, but this is one way of working.

Hope this helps you.

+2


source







All Articles