Swift使用Contacts Framework,使用Identifier进行搜索以进行匹配

时间:2017-02-22 13:51:40

标签: swift predicate cncontact cncontactstore

尝试获取提供联系人标识符的联系人匹配但未成功。我希望返回联系人,然后使用与之关联的图像。我得到一个零比赛。谢谢。我从演示中获得了这段代码,我对编程很陌生

bootstrap.css

1 个答案:

答案 0 :(得分:3)

我解决了它:),我删除了dispatch_async的东西,现在正常工作:这里是固定代码。

func getContactFromID(contactID: String) -> CNContact {

        AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
            if accessGranted {

                let predicate = CNContact.predicateForContactsWithIdentifiers([contactID])

                let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey]

                var contacts = [CNContact]()
                var message: String!

                let contactsStore = AppDelegate.getAppDelegate().contactStore

                do {
                    contacts = try contactsStore.unifiedContactsMatchingPredicate(predicate, keysToFetch: keys)

                    if contacts.count == 0 {
                        message = "No contacts were found matching the given name."
                    }
                }
                catch {
                    message = "Unable to fetch contacts."
                }

                 self.contact = contacts[0]
            }
        }
        return self.contact
    }