iOS 6: correctly use ABAddressBookRequestAccessWithCompletion
In iOS 6 is always necessary to verify that the application has access to the contacts directory.
The application works in iOS 5.x, but does not work in iOS 6.x. ABAddressBookGetPersonCount returns -1. This is caused by the fact that you do not work correctly with contacts, and applications can not access it.
Example of use:
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//create a new variable addressbook ABAddressBookRef addressBook = ABAddressBookCreate(); //boolean variable stands for directory access __block BOOL isaccess = NO; //ABAddressBookRequestAccessWithCompletion use to request access to address book data //this function is availabile in iOS 6.0 //iOS 6 if(ABAddressBookRequestAccessWithCompletion != NULL) { //Create the semaphore, specifying the initial pool size dispatch_semaphore_t sema = dispatch_semaphore_create(0); //ask to grand or deny access ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { isaccess = granted; dispatch_semaphore_signal(sema); }); dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_release(sema); } //iOS 5 or older else { isaccess = YES; } if (isaccess) { //here you can working with address book } |
Posted on 3 April 2013