私は電話機のすべての連絡先リストとその名前と電話番号をswift 3.0とxcode 8.0で取得したいと考えています。以下 は、コードCNPropertyNotFetchedExceptionアプリケーションがクラッシュしました
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) ?? "???")
}
}
}
私はこのアプリを実行すると、それがクラッシュし、私は私が間違って取得していますどこか分からないです。 誰でも私を助けることができます..それは感謝されます。
に
CNContactPhoneNumbersKey
を追加する必要がありますか?あなたはどんなメッセージを受けていますか? –