我正在尝试构建具有数字查找 friend 功能的 iPhone 应用程序
我可以从 Digits 中获取匹配 digitUserID
的列表。
现在我正在努力匹配 UserID
和 CNContacts
。
请指出任何例子来处理这个问题。
作为更新:
do
{
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey])) {
(contact, cursor) -> Void in
self.results.append(contact)
}
}
catch{
print("Handle the error please")
}
上面我已经设法获得了所有联系人,但我不知道如何将电话号码过滤器传递给它并获得与 CNContact
的精确联系人匹配
最佳答案
理想情况下,人们会期望 CNContactFetchRequest
的 predicate
来完成这项工作,但那(仍然;argh)只接受用 定义的一小部分谓词>CNContact
(例如 CNContact
predicateForContacts(matchingName:)
或 predicateForContacts(withIdentifiers:)
。它甚至不接受 block 基于 NSPredicate
.
所以,你必须通过枚举,自己寻找匹配项,例如
let request = CNContactFetchRequest(keysToFetch: [
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactMiddleNameKey as CNKeyDescriptor,
CNContactEmailAddressesKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor
])
do {
try contactStore.enumerateContacts(with: request) { contact, stop in
for phone in contact.phoneNumbers {
// look at `phone.value.stringValue`, e.g.
let phoneNumberDigits = String(phone.value.stringValue.characters.filter { String($0).rangeOfCharacter(from: CharacterSet.decimalDigits) != nil })
if phoneNumberDigits == "8885551212" {
self.results.append(contact)
return
}
}
}
} catch let enumerateError {
print(enumerateError.localizedDescription)
}
关于匹配“数字 UserID”,我不知道那个标识符是什么(它是 Contacts 框架标识符还是 Digits 自己的标识符?)。
关于swift - 匹配CNcontact和Digits Find Friends Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987763/