【问题标题】:Swift 3 / Xcode 8 - CNContact [access] <Private>Swift 3 / Xcode 8 - CNContact [访问] <私人>
【发布时间】:2016-07-07 08:55:47
【问题描述】:

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

var addressBookStore = CNContactStore()

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

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

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

提前致谢

【问题讨论】:

    标签: ios swift swift3 xcode8 cncontactstore


    【解决方案1】:

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

    重要

    在 iOS 10.0 或之后链接的 iOS 应用程序必须包含在其 Info.plist 中 归档它需要的数据类型的使用描述键 访问,否则会崩溃。要专门访问联系人数据,它必须 包括 NSContactsUsageDescription。

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

    然后您将获得授权对话框。没有这个键应用会崩溃。

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

    【讨论】:

    • 这样一个简单的解决方案。我不敢相信我错过了!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2016-11-21
    • 1970-01-01
    • 2017-02-08
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多