[英]CNPropertyNotFetchedException App Crashed
我想獲取電話的所有聯系人列表及其名稱以及swift 3.0和xcode 8.0中的電話號碼。 下面是代碼
func get_all_user_contacts()
{
let status = CNContactStore.authorizationStatus(for: .contacts)
if status == .denied || status == .restricted
{
presentSettingsActionSheet()
return
}
// open it
let store = CNContactStore()
store.requestAccess(for: .contacts) { granted, error in
guard granted else
{
DispatchQueue.main.async {
self.presentSettingsActionSheet()
}
return
}
// get the contacts
var contacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey as NSString, CNContactFormatter.descriptorForRequiredKeys(for: .fullName)])
do
{
try store.enumerateContacts(with: request)
{ contact, stop in
contacts.append(contact)
}
}
catch
{
print(error)
}
// do something with the contacts array (e.g. print the names)
let formatter = CNContactFormatter()
formatter.style = .fullName
for contact in contacts
{
let MobNumVar = ((contact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue
print(MobNumVar)
print(formatter.string(from: contact) ?? "???")
}
}
}
當我運行這個應用程序時,它崩潰了,我不知道我在哪里弄錯了。 任何人都可以幫助我..將不勝感激。
您正在要求鑰匙
• CNContactIdentifierKey
• CNContactFormatter.descriptorForRequiredKeys(for: .fullName)
…但隨后您嘗試訪問contact.phoneNumber
。
您只能訪問keysToFetch
指定的keysToFetch
,因此您需要將CNContactPhoneNumbersKey
添加到該數組中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.