知道为什么有些 CNContacts
返回的电话号码是空的吗?
我不断得到这样的输出:
"phoneNumbers=(not fetched), emailAddresses=(not fetched),"
例如,当我从模拟器的内置联系人应用程序中获取联系人时,我得到了混合结果:
“John Appleseed”
确实返回其电话号码,但“Kate Bell”
不是——尽管她显然有电话号码。
这很奇怪,因为我肯定在我的获取请求中使用了 CNContactPhoneNumbersKey
。
这是我的代码:
let fetchKeys = [ CNContactEmailAddressesKey, CNContactPhoneNumbersKey,
CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName) ]
let contactFetchRequest = CNContactFetchRequest(keysToFetch: fetchKeys)
do {
try self.contactsStore.enumerateContactsWithFetchRequest(contactFetchRequest) {
fetchedContact, stop in
print("fetched Contact is: \n\(fetchedContact.description)")
}
}
正如我提到的,输出显示有时获取的联系人会返回电话号码,有时则不会。电子邮件也是如此:有时会被退回,有时不会。
这在模拟器中和我在 iPhone 上运行时都会发生。
知道这里会发生什么吗?
最佳答案
在联系人应用程序中添加电话号码时,他们有时会错过标签(例如“家庭”或“电话”......) 如果标签为空,则不显示数字。一种解决方法是为缺失的标签显式设置默认标签。一个简单的例子:
for phoneNumber in contact.phoneNumbers {
guard let phone = phoneNumber.value as? CNPhoneNumber
else { continue }
let phoneLabel = phoneNumber.label == nil ? "DefaultLabel" : phoneNumber.label!
phoneNumbers.append((phone.stringValue,phoneLabel))
}
关于iOS 9 - 获取的联系人返回空电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36441274/