To access contacts, we should apply a filter of type NSPredicate
to our contactStore variable which we defined it in Authorizing Contact Access example. For example, here we want to sort out contacts with name matching with our own:
let predicate = CNContact.predicateForContactsMatchingName("Some Name")
NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"Some Name"];
Here, we want to fetch the contact's first name, last name and profile image:
let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactImageDataKey]
do {
let contacts = try contactStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)
} catch let error as NSError {
//...
}
print(contacts[0].givenName)
print(contacts[1].familyName)
let image = contacts[2].imageData