问题
There is an odd error in my application that I can't find any workarounds/fixes for. For some reason, I'm able to save an address to a contact that isn't unified with a social profile (Facebook, Twitter, etc). However, when I try to add an address for my contact that is unified with Facebook or Twitter I get a weird save error:
The operation couldn’t be completed. (CNErrorDomain error 500.)
Here is some of the code that I'm using:
if mutableContact.isKeyAvailable(CNContactPostalAddressesKey) {
var postalAddresses = [CNLabeledValue<CNPostalAddress>]()
for address in self.contactAddresses {
let postalAddress: CNLabeledValue<CNPostalAddress> = CNLabeledValue(label: CNLabelOther, value: address)
postalAddresses.append(postalAddress)
}
mutableContact.postalAddresses = postalAddresses
}
let saveRequest = CNSaveRequest()
if isNewContact {
saveRequest.add(mutableContact, toContainerWithIdentifier: nil)
} else {
saveRequest.update(mutableContact)
}
do {
try contactStore.execute(saveRequest)
} catch let error as NSError {
print(error.localizedDescription)
let alertController = UIAlertController(title: "Failed to save/update contact!", message: "Unfortunatly, the app couldn't add or make modifications to your contact. Please try again or use the Contacts app to preform changes.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay", style: .cancel) {
action in
self.dismiss(animated: true, completion: nil)
}
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
回答1:
Ok, so I have gotten a response from Apple and this behavior is intended. Developers should detect this policy violation and then offer to create a new contact and then link the two contacts.
回答2:
I understand why Apple do it, as the social account does not store all the same data as (say) an iCloud contact, so you need to keep those values somewhere else, (and you can't convert the contact to iCloud as the social account will lose its data.)
I don't think you can CHOOSE to link contacts programmatically, though, can you?
As I understand it iOS will decide whether two contacts are the same- probably when more than one value matches (?) You can detect whether two given contacts are linked together.
来源:https://stackoverflow.com/questions/40568549/saving-contact-address-to-unified-contact-results-in-cnerrordomain-error-500