Swift 3 / Xcode 8 - CNContact [访问] <Private>

Swift 3 / Xcode 8 - CNContact [access] <Private>

我的代码在尝试请求访问 CNContactStore 时立即崩溃。如果这是 beta 问题,有什么想法吗?

var addressBookStore = CNContactStore()

addressBookStore.requestAccess(for: .contacts) { (granted, error)

// 崩溃时触发此控制台消息 - Messenger[836:1175155] [access] private

崩溃发生在这一行,甚至阻止打印错误!

提前致谢

这里建议:https://developer.apple.com/reference/contacts

Important

An iOS app linked on or after iOS 10.0 must include in its Info.plist file the usage description keys for the types of data it needs to access or it will crash. To access Contacts data specifically, it must include NSContactsUsageDescription.

您必须在 Info.plist 文件中添加 NSContactsUsageDescription 键

然后你会看到授权对话框。没有这个密钥应用程序将崩溃。

let addressBookStore = CNContactStore()

addressBookStore.requestAccess(for: CNEntityType.contacts) { (isGranted, error) in
    print(isGranted)
    print(error)
}