Accessing Contacts
suggest changeApplying a filter
To access contacts, apply an NSPredicate to the contactStore from the Authorizing Contact Access example. For example, find contacts whose name matches a string:
Swift
let predicate = CNContact.predicateForContactsMatchingName("Some Name")
Objective-C
NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"Some Name"];
Specifying keys to fetch
Here, we want to fetch the contact’s first name, last name and profile image:
Swift
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]
Fetching contacts
Swift
do {
let contacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
} catch let error as NSError {
//...
}
Accessing contact details
Swift
contacts may be empty, so don't subscript it blindly. These properties belong to one contact, not to three different indexes:
if let contact = contacts.first {
print(contact.givenName)
print(contact.familyName)
let image = contact.imageData
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents