O'Reilly logo

Programming iOS 8 by Matt Neuburg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 18. Address Book

The user’s address book, which the user sees through the Contacts app, is effectively a database that your code can access programmatically through the Address Book framework. You’ll need to import AddressBook. This is, unfortunately, an archaic C API without memory management information, so you’re going to be intervening constantly to help manage memory.

A user interface for interacting with the address book is provided by the Address Book UI framework. You’ll need to import AddressBookUI.

Address Book Database

The address book is an ABAddressBook object obtained by calling ABAddressBookCreateWithOptions. There are in fact no options to pass. You’re probably going to need a single persistent reference to the address book, so it is tempting to assign this reference to a property as its default value:

var adbk : ABAddressBook =
    ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()

However, there’s a serious problem with that approach. Access to the address book database requires user authorization. If the authorization status is .NotDetermined, the preceding code does a very bad thing: it fails silently, yielding a non-nil result and not reporting an error. You thus end up with an ABAddressBook reference that is completely invalid and useless — and you have no way of finding out that this has happened. It is crucial, therefore, to verify authorization status independently, as I shall now describe.

Address Book Authorization

Address book authorization is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required