Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios 8 : Access contacts/AddressBook error

Tags:

ios8

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.

like image 891
user1259574 Avatar asked Feb 17 '26 16:02

user1259574


1 Answers

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.

like image 74
APesate Avatar answered Feb 21 '26 15:02

APesate