如何使用CNContact.predicateForContacts检索所有联系人? [英] How to retrieve all contacts using CNContact.predicateForContacts?

查看:258
本文介绍了如何使用CNContact.predicateForContacts检索所有联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码可以正常工作,但前提是您在predicateForContacts参数中指定了名称.

So I have this code which works fine, but only if you have specified a name in the predicateForContacts parameter.

func retrieveContactsWithStore(store: CNContactStore) {
    do {
           let predicate = CNContact.predicateForContacts(matchingName: "John")
           let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as [Any]

           let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
           self.objects = contacts
           DispatchQueue.main.async(execute: { () -> Void in
               self.myTableView.reloadData()
           })
       } catch {
           print(error)
       }
}

我想检索通讯录中列出的所有人员的姓名.

I'd like to retrieve all the names of the people listed on address book.

推荐答案

我想检索通讯录中列出的所有人员的姓名.

I'd like to retrieve all the names of the people listed on address book.

形成一个 CNContactFetchRequest ,指定您想要的键是名称,然后调用 enumerateContacts(with:usingBlock:) .

Form a CNContactFetchRequest specifying that the keys you want are names, and call enumerateContacts(with:usingBlock:).

    let req = CNContactFetchRequest(keysToFetch: [
        CNContactFamilyNameKey as CNKeyDescriptor,
        CNContactGivenNameKey as CNKeyDescriptor
    ])
    try! CNContactStore().enumerateContacts(with: req) {
        contact, stop in
        print(contact) // in real life, probably populate an array
    }

这篇关于如何使用CNContact.predicateForContacts检索所有联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆