Chapter 8. Contacts

The Contacts framework is for those who want to import, show, select, modify, and save contacts on a user’s iOS device. This framework is fully compatible with Swift’s lingo and is very easy to work with. At the heart of the Contacts framework is the CNContact object, which represents a contact. You get access to the Contacts database using the CNContactStore class.

Every time you want to access the address book, whether you are trying to create a new contact or fetch an existing one, you need to ensure that you have sufficient access to the address book. You can check your access privileges using the authorizationStatus(for:) class method of your contact store. This method takes in one parameter of type CNEntityType. You can pass the value of Contacts to this method, for instance, to ask for access to the user’s contacts. If you do not have access, you can use the requestAccess(for:completionHandler:) method of your contact store to request access.

The concept of a partial contact is important enough to cover now as well. A partial contact is a contact whose properties have not all been fetched from the store yet. For instance, perhaps you can fetch only a contact’s first and last name, not her profile photo or email addresses. This is a partial contact. A partial contact’s other information—such as email addresses—that has not been fetched yet can later be fetched from the store using her identifier (part of the CNContact object).

Some of the classes that ...

Get iOS 10 Swift Programming Cookbook 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.