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.

An interface for letting the user interact with the contacts database from within your app is provided by the Contacts UI framework. You’ll need to import ContactsUI.

Access to the contacts database requires user authorization. You’ll use the CNContactStore class for this. To learn what the current authorization status is, call the class method authorizationStatus(for:) with a CNEntityType of .contacts. To ask the system to put up the authorization request alert if the status is .notDetermined, call the instance method requestAccess(for:completionHandler:). The Info.plist must 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). See “Checking for Authorization” for detailed consideration of authorization strategy and testing.

Contact Classes

Here are the chief object types you’ll be concerned with when you work with the user’s contacts:

CNContactStore

The user’s database of contacts is accessed through an instance of the CNContactStore class. You do not need to keep a reference to an instance of this class. When you want to fetch a contact from the database, or when you want to save a created or modified contact into the ...

Get Programming iOS 11 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.