Alexander Sergeev's blog

Dealing with AdressBook on iOS 6.0: ABAddressBookRequestAccessWithCompletion

As you know, we have to use ABAddressBookRequestAccessWithCompletion call in order to get access to an Address Book in iOS 6.0 and higher:

void ABAddressBookRequestAccessWithCompletion (
ABAddressBookRef addressBook,
ABAddressBookRequestAccessCompletionHandler completion
);

(from iOS developer documentation)

It is not oblivious that the completion handler will be called on the arbitrary queue:

Discussion
Use this function to request access to address book data. This call will not block while the user is being asked for access, allowing your app to continue running. Until access has been granted, any address book references your app has will not contain any data, and any attempt to modify data will fail with an error type of kABOperationNotPermittedByUserError. The user is only asked for permission the first time you request access. Later calls use the permission granted by the user.

The completion handler is called on an arbitrary queue. If your app uses an address book throughout the app, you are responsible for ensuring that all usage of that address book is dispatched to a single queue to ensure correct thread-safe operation.

so you have to synchronize the execution somehow.

The following approach, introduced by Josephus here does not work for me: it locks the application completely if called on a main thread – it looks like the blocking of a main thread interferes with a system Address Book alert. Thus, we have decided to use the following code to solve the problem:


if (isIOS6orHigher) {
// System alert for iOS >= 6.0
ABAddressBookRequestAccessWithCompletion(addressBook_, ^(bool granted, CFErrorRef error) {
// We are on arbitrary queue
dispatch_async(dispatch_get_main_queue(), ^{
// We are on main queue
[self processAccessResponce:granted];
});
});
} else {
// Custom alert view for iOS < 6.0
...
}

 

Share

Category: IT

Tagged: , , ,

One Response

  1. Soviet Union Marshal says:

    Thank you! It’s very helpful for me.
    So, I can add a little improvement. Sometimes, it’s need to know the
    AB status and method ABAddressBookGetAuthorizationStatus gives information directly without block.

Leave a Reply

You must be logged in to post a comment.




Forgot?
Register
Facebook login by WP-FB-AutoConnect