Is it possible to fetch a particular contact information or the list of contact informations based on organization name or the job title using both AddressBook and contacts framework ??
I don't think so you can fetch the contacts from CNContact store by just giving organization name or job title in predicate. You should include organization name and job title in keys to fetch and then iterate the contact list again. Check the code snippet. I hope it helps. Thanks.
func fetchContacts()
{
let contactStore = CNContactStore()
var allContainers : [CNContainer] = []
var allContacts : [CNContact] = []
//you can use one of these/ all keys to filter contacts
let keysToFetch = [CNContactGivenNameKey, CNContactOrganizationNameKey, CNContactJobTitleKey]
var OrganizationArray = [CNContact]()
do{
// _______________ Fetch all the Containers_________________________________
allContainers = try contactStore.containersMatchingPredicate(nil)
}
catch{
print(error)
}
for container in allContainers{
let fetchPredicate = CNContact.predicateForContactsInContainerWithIdentifier(container.identifier)
do{
//____________Fetch all the contacts corresponding to every Container______
let containerResults = try contactStore.unifiedContactsMatchingPredicate(fetchPredicate, keysToFetch: keysToFetch)
// allContacts.appendContentsOf(containerResults)
for contactRec in containerResults {
if contactRec.organizationName != "" {
OrganizationArray.append(contactRec)
}
}
}
catch{
print(error)
}
}
}