gpt4 book ai didi

ios - 如何在没有过滤器的情况下从设备中检索所有 CNContactStore

转载 作者:行者123 更新时间:2023-11-30 12:41:25 24 4
gpt4 key购买 nike

我正在尝试插入var 联系人:[CNContact] = []var store = CNContactStore() 但我没有找到适合这项工作的代码,我找到了这个函数,我需要为其命名

func findContactsWithName(name: String) {
AppDelegate.sharedDelegate().checkAccessStatus({ (accessGranted) -> Void in
if accessGranted {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
do {
let predicate: NSPredicate = CNContact.predicateForContactsMatchingName(name)
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactBirthdayKey, CNContactViewController.descriptorForRequiredKeys()]
self.contacts = try self.store.unifiedContactsMatchingPredicate(predicate, keysToFetch:keysToFetch)


self.tableView.reloadData()
}
catch {
print("Unable to refetch the selected contact.")
}
})
}
})
}

我想插入 self.contacts 所有记录,而不仅仅是同名的记录

最佳答案

更新

根据OP的评论,请尝试以下基于CNContactFetchRequest的API来检索所有联系人而不使用过滤器。我在后台线程上运行此程序,以减少大量联系人可能出现的任何问题。

func findContactsOnBackgroundThread ( completionHandler:(contacts:[CNContact]?)->()) {

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), { () -> Void in

let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),CNContactPhoneNumbersKey] //CNContactIdentifierKey
let fetchRequest = CNContactFetchRequest( keysToFetch: keysToFetch)
var contacts = [CNContact]()
CNContact.localizedStringForKey(CNLabelPhoneNumberiPhone)

fetchRequest.mutableObjects = false
fetchRequest.unifyResults = true
fetchRequest.sortOrder = .UserDefault

let contactStoreID = CNContactStore().defaultContainerIdentifier()
print("\(contactStoreID)")


do {

try CNContactStore().enumerateContactsWithFetchRequest(fetchRequest) { (contact, stop) -> Void in
//do something with contact
if contact.phoneNumbers.count > 0 {
contacts.append(contact)
}

}
} catch let e as NSError {
print(e.localizedDescription)
}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
completionHandler(contacts: contacts)

})
})
}

一般来说,在使用 CNContactFetchRequest 时,您通常会将谓词设置为 nil 以检索所有联系人。类而不是代码中描述的类。

注意

如果您想使用现有的 API,那么我建议将谓词设置为 true:

 NSPredicate(value: true)

这应该会使所有联系人返回。如果这不起作用,请考虑切换到 CNConctactFetchRequest API 来枚举联系人。在这种情况下,您可以将谓词设置为 nil 以获取所有联系人(使用 CNConctactFetchRequest)。

CNContactFetch-Predicate

您可以这样修改现有方法:

func findContacts()->[CNContact] {
AppDelegate.sharedDelegate().checkAccessStatus({ (accessGranted) -> Void in
if accessGranted {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
do {
let predicate: NSPredicate = NSPredicate(value: true)
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactBirthdayKey, CNContactViewController.descriptorForRequiredKeys()]
self.contacts = try self.store.unifiedContactsMatchingPredicate(predicate, keysToFetch:keysToFetch)


self.tableView.reloadData()
}
catch {
print("Unable to refetch the selected contact.")
}
})
}
})
}

并使用:

let contacts = findContacts()

Apple 有一个更简单的示例:

let store = CNContactStore()
let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName("Appleseed"), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey])

对于您的用例,您可以尝试像这样修改Apple示例:

//Use the reference to look up additional keys constants that you may want to fetch
let store = CNContactStore()
let contacts = try store.unifiedContactsMatchingPredicate(NSPredicate(value: true), keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey])

More Apple Samples for the Contacts Framework

关于ios - 如何在没有过滤器的情况下从设备中检索所有 CNContactStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42184489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com