Address Book Tutorial in iOS

In this Address Book Tutorial in iOS, learn how to add and edit contacts in a fun app about pets. By Evan Dekhayser.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Multithreading

There is still a hidden issue with the code as it is right now. If you look at the documentation for ABAddressBookRequestAccessWithCompletion (which is called in petTapped:) the completion is called on an arbitrary queue. In other words, it may be called on a thread besides the main thread.

If there is one thing you should know about multithreading, it’s that the user interface can only be used on the main thread. You have to make sure that anything affecting the user interface (presenting UIAlertView?) is called on the main thread.

This is easy to accomplish with the following code. Insert this at the beginning of the completion of ABAddressBookRequestAccessWithCompletion.

dispatch_async(dispatch_get_main_queue(), ^{

});

This runs the block on the main thread so you can use the user interface. To learn more about multithreading, read this tutorial.

Cut and paste the code from inside the completion handler into the dispatch_async block, to make the call look like this:

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
  dispatch_async(dispatch_get_main_queue(), ^{
    if (!granted){ 
      //4
      UIAlertView *cantAddContactAlert = [[UIAlertView alloc] initWithTitle: @"Cannot Add Contact" message: @"You must give the app permission to add the contact first." delegate:nil cancelButtonTitle: @"OK" otherButtonTitles: nil];
      [cantAddContactAlert show];
      return;		
    }
    //5
    [self addPetToContacts:sender];
  });
});

This is the best way for the app to ask permission to use the Address Book. Note that a best practice is to ask for permission only when you actually need to use it — if you ask for permission when the app first launches, the user may be suspicious because they won’t understand why you need to use the Address Book.

One issue with ABAddressBookRequestAccessWithCompletion is that once the user gives the app permission, sometimes there is a 5-10 second delay until the completion is called. This can make it seem like the app’s frozen when it’s adding the contact. In most cases, this is not too much of an issue.

Your PetBook app is now fully functional, and I know you can’t wait to text your new furry friends right away!

Note: Sorry to disappoint all the raving Shippo fans, but the numbers listed here are fake :]

Note: Sorry to disappoint all the raving Shippo fans, but the numbers listed here are fake :]

Where To Go From Here?

Here is the finished example project from this Address Book tutorial.

You can do many other cool things with the Address Book API. In this tutorial, you learned how to create a new record. As an extension of this tutorial, try modifying pre-existing contacts in the Address Book.

Along with this framework, there is also an AddressBookUI.framework that has several convenient classes for modifying the address book. What these can do is give your app functionality similar to the contacts app.

If you have any questions or comments regarding this tutorial or the Address Book API, please join the discussion in the comments below!

Evan Dekhayser

Contributors

Evan Dekhayser

Author

Over 300 content creators. Join our team.