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



是否可以使用地址簿和联系人框架根据组织名称或职位获取特定的联系信息或联系信息列表?

我不认为您可以通过在谓词中提供组织名称或职位来从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)
            }
        }
}

最新更新