Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Contact Address to Unified Contact results in (CNErrorDomain error 500)

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)
    }
like image 733
Harish Avatar asked Nov 12 '16 22:11

Harish


1 Answers

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.

like image 69
Harish Avatar answered Nov 14 '22 06:11

Harish