【问题标题】:CNContactStore execute failsCNContactStore 执行失败
【发布时间】:2019-02-22 00:07:04
【问题描述】:

我正在研究如何使用联系人框架,但是一些用于创建联系人的相当简单的代码因意外结果而失败。这是我的代码:

    let Store = CNContactStore()
    Store.requestAccess(for: .contacts, completionHandler:{ success, error in
        if success {
            let Contact = CNMutableContact()
            Contact.givenName = "Dave"
            Contact.familyName = "Nottage"
            let SaveRequest = CNSaveRequest()
            SaveRequest.add(Contact, toContainerWithIdentifier: nil)
            do {
                try Store.execute(SaveRequest)
                print("Success")
            }
            catch let error as NSError {
                print(error.localizedDescription)
            }
        } else {
            print("No access")
        }
    })

..这是结果:

2019-02-22 10:30:56.050344+1030 ContactsTest[30329:25254955] [default] Unable to load Info.plist exceptions (eGPUOverrides)
2019-02-22 10:30:57.973724+1030 ContactsTest[30329:25254955] Could not get real path for Address Book lock folder: open() for F_GETPATH failed.
2019-02-22 10:30:57.973954+1030 ContactsTest[30329:25254955] Unable to open file lock: <ABProcessSharedLock: 0x600001752ac0: name=(null), lockFilePath=(null), localLock=<NSRecursiveLock: 0x600002914a80>{recursion count = 0, name = nil}, fileDescriptor=-1> Error Domain=NSPOSIXErrorDomain Code=14 "Bad address" UserInfo={ABFileDescriptor=-1}
The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)

有什么可能导致这种情况的想法吗?

编辑:另请注意,这是为 macOS 10.14 SDK 编译的,并且在 macOS 10.14.3 上运行

【问题讨论】:

    标签: swift macos contacts-framework


    【解决方案1】:

    答案是在Capabilities的App Sandbox部分勾选App Data的Contacts复选框,打开App Sandbox的开关。

    【讨论】:

      【解决方案2】:

      确保您在 Info.plist 中添加了密钥 NSContactsUsageDescription

      请参考link

      重要

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2019-04-17
        • 2020-03-07
        • 1970-01-01
        • 1970-01-01
        • 2019-05-02
        相关资源
        最近更新 更多