根据组织名称或职位获取 iOS 联系人列表

Fetch list of iOS contacts based on the organization name or the jobtitle

是否可以同时使用 AddressBook 和联系人框架获取特定的联系人信息或基于组织名称或职位的联系人信息列表?

我不认为你可以通过在谓词中提供组织名称或职位来从 CNContact 存储中获取联系人。您应该在要获取的键中包含组织名称和职位,然后再次迭代联系人列表。检查代码片段。我希望它有所帮助。谢谢

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)
            }
        }
}