Chapter 19. Contacts
The user’s contacts constitute a database. The user can interact with this database through the Contacts app. Your code can access the user’s contacts database programmatically through the Contacts framework. You’ll need to import Contacts
. An interface similar to the Contacts app 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
. (This is a curious requirement, as .contacts
is the only CNEntityType!) 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 meaningful 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 a discussion of authorization strategy.
Contact Classes
Here are the chief object types you’ll be concerned with when you work with the user’s contacts:
- CNContactStore
-
The contacts database is accessed through an instance of the CNContactStore class. You do not need to keep a reference to an instance of this ...
Get Programming iOS 14 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.