Hi, I'm writing a command line program with Swift Package Manager that uses Contacts API to automatically add phonetic names for the contacts. However, I found I was unable to do so for contacts that has notes. First, when I was trying to enumerate the contacts, there is a strange error output:
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
According to this document, I need to add the notes entitlement only if I was requesting the note field with CNContactNoteKey. However, I was not requesting it and I still get that "Attempt to read notes by an unentitled app" error.
Here is my code for enumerating the contacts:
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)")
}
And this is how I update the contacts later:
let saveRequest = CNSaveRequest()
contact.phoneticFamilyName = phoneticFamilyName
contact.phoneticMiddleName = phoneticMiddleName
contact.phoneticGivenName = phoneticGivenName
saveRequest.update(contact)
do {
try store.execute(saveRequest)
} catch {
print("Fail to execute store request: \(error)")
}
This code works as expected for contacts without the note field. But for contact with a non-empty note field, the saveRequest fails and outputs the following error:
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)" ({
})
2023-01-22 18:58:36.201308+0800 PhoneticNames[18591:98344] [plugin] CDX_AB_GetGroupCollectionPath: nil group
2023-01-22 18:58:36.201353+0800 PhoneticNames[18591:98344] [plugin] CDX_AB_GetGroupCollectionPath: nil group
In debugging mode inside Xcode, the contact will still be updated with phoetic names. But if I execute the compiled release binary in terminal, the output will be:
CoreData: error: Unhandled error occurred during faulting: Error Domain=NSCocoaErrorDomain Code=134092 "(null)" ({
})
Fail to execute store request: Error Domain=NSCocoaErrorDomain Code=134092 "(null)" UserInfo={NSUnderlyingException=Unhandled error (NSCocoaErrorDomain, 134092) occurred during faulting and was thrown: Error Domain=NSCocoaErrorDomain Code=134092 "(null)", NSUnderlyingError=0x600002ee05d0 {Error Domain=NSCocoaErrorDomain Code=134092 "(null)"}}
The contact will not be updated in this case.
I looked for all the documents but couldn't find anything about NSCocoaErrorDomain Code 134092. Can you please give me some clues about the aforementioned behaviors?