Chapter 18. Contacts

The user’s contacts, which the user sees through the Contacts app, constitute a database that your code can access programmatically through the Contacts framework. You’ll need to import Contacts.

A user interface for interacting with the contacts database is provided by the Contacts UI framework. You’ll need to import ContactsUI.

Note

The Contacts framework, which is new in iOS 9, replaces the Address Book framework and the Address Book UI framework. The Address Book framework was an archaic C API without memory management information, so it was almost impossible to use in Swift, and it wasn’t all that usable in Objective-C either. The Address Book framework is not discussed in this edition.

Contacts Authorization

Access to the contacts database requires authorization from the user. To learn whether you have authorization, call the CNContactStore class method authorizationStatusForEntityType:; there is only one possible entity type (CNEntityType), namely .Contacts. To request authorization if your status is .NotDetermined, call the CNContactStore instance method requestAccessForEntityType:completionHandler:. The first time you do this with a .NotDetermined status, the user will see the system alert requesting access on your behalf. Your Info.plist should contain some text that the system authorization request alert can use to explain why your app wants access; the relevant key is “Privacy — Contacts Usage Description” (NSContactsUsageDescription).

A possible authorization ...

Get Programming iOS 9 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.