Saving Contact Address to Unified Contact results

2019-04-07 15:48发布

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)
    }

2条回答
Rolldiameter
2楼-- · 2019-04-07 16:30

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.

查看更多
beautiful°
3楼-- · 2019-04-07 16:37

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.

查看更多
登录 后发表回答