根据组织名称或作业标记获取iOS联系人列表

时间:2016-05-23 13:03:18

标签: ios fetch contacts addressbook

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

1 个答案:

答案 0 :(得分:2)

我不这么认为你可以通过在谓词中给出组织名称或职位来从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)
            }
        }
}