Swift:执行联系人存储的 CNSaveRequest 时出现“NSCocoaErrorDomain Code=134092”

Jun*_*hen 6 cocoa swift swift-package-manager

我正在使用 Swift Package Manager 编写一个命令行程序,该程序使用 Contacts API 自动添加联系人的拼音名称。但是,我发现我无法对有注释的联系人执行此操作。首先,当我尝试枚举联系人时,出现一个奇怪的错误输出:

2023-01-22 18:58:36.085525+0800 PhoneticNames[18591:98356] [core] Attempted to register account monitor for types client is not authorized to access: {(
    "com.apple.account.CardDAV",
    "com.apple.account.Exchange",
    "com.apple.account.LDAP"
)}
2023-01-22 18:58:36.085580+0800 PhoneticNames[18591:98356] [accounts] CNAccountCollectionUpdateWatcher 0x6000003ccc40: Store registration failed: Error Domain=com.apple.accounts Code=7 "(null)"
2023-01-22 18:58:36.085606+0800 PhoneticNames[18591:98356] [accounts] CNAccountCollectionUpdateWatcher 0x6000003ccc40: Update event received, but store registration failed. This event will be handled, but the behavior is undefined.
2023-01-22 18:58:36.136236+0800 PhoneticNames[18591:98344] [api] Attempt to read notes by an unentitled app
Run Code Online (Sandbox Code Playgroud)

根据此文档,仅当我使用 CNContactNoteKey 请求注释字段时,我才需要添加注释权利。但是,我并没有请求它,但我仍然收到“尝试通过未授权的应用程序读取笔记”错误。

这是我用于枚举联系人的代码:

2023-01-22 18:58:36.085525+0800 PhoneticNames[18591:98356] [core] Attempted to register account monitor for types client is not authorized to access: {(
    "com.apple.account.CardDAV",
    "com.apple.account.Exchange",
    "com.apple.account.LDAP"
)}
2023-01-22 18:58:36.085580+0800 PhoneticNames[18591:98356] [accounts] CNAccountCollectionUpdateWatcher 0x6000003ccc40: Store registration failed: Error Domain=com.apple.accounts Code=7 "(null)"
2023-01-22 18:58:36.085606+0800 PhoneticNames[18591:98356] [accounts] CNAccountCollectionUpdateWatcher 0x6000003ccc40: Update event received, but store registration failed. This event will be handled, but the behavior is undefined.
2023-01-22 18:58:36.136236+0800 PhoneticNames[18591:98344] [api] Attempt to read notes by an unentitled app
Run Code Online (Sandbox Code Playgroud)

这就是我稍后更新联系人的方式:

let keys: [CNKeyDescriptor] = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
                               CNContactFormatter.descriptorForRequiredKeys(for: .phoneticFullName)]
let fetchRequest = CNContactFetchRequest(keysToFetch: keys as [CNKeyDescriptor])
fetchRequest.mutableObjects = true
do {
    try store.enumerateContacts(with: fetchRequest) { (contact, stop) in
        contacts.append(contact.mutableCopy() as! CNMutableContact)
    }
} catch {
    print("unable to list contacts. \(error)")
}
Run Code Online (Sandbox Code Playgroud)

对于没有注释字段的联系人,此代码按预期工作。但是对于与非空注释字段的联系,saveRequest 失败并输出以下错误:

2023-01-22 18:58:36.197026+0800 PhoneticNames[18591:98344] [error] error: Unhandled error occurred during faulting: Error Domain=NSCocoaErrorDomain Code=134092 "(null)" ({
})
CoreData: error: Unhandled error occurred during faulting: Error Domain=NSCocoaErrorDomain Code=134092 "(null)" ({
})
Run Code Online (Sandbox Code Playgroud)

我使用的是 Swift 5.7、Xcode 14.2、macOS 13.1。我查找了所有文档,但找不到任何有关 NSCocoaErrorDomain Code 134092 的信息。

当我不请求笔记时,我不应该收到错误“尝试通过未授权的应用程序阅读笔记”。我应该能够成功执行saveRequest不修改注释字段的操作。有谁知道这些错误是怎么来的?