For now (iOS 9.0) it seems that no predicates (see CNContact Predicates) are available to filter contacts by email address!
You can't write a custom predicate to filter contacts, as the doc says:
"Note that generic and compound predicates are not supported by the Contacts framework"
But of course you can do it "manually", I show you an example using fast enumeration:
let contactStore = CNContactStore()
fetchRequest.unifyResults = true //True should be the default option
do {
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey])) {
(contact, cursor) -> Void in
if (!contact.emailAddresses.isEmpty){
//Add to your array
}
}
}
catch{
print("Handle the error please")
}
Note:
In newer versions of Swift, the call to contactStore.enumerateContactsWithFetchRequest
needs to be updated to:
try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey] as [CNKeyDescriptor])) {
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…