我的应用程序中有一个奇怪的错误,我找不到任何变通方法/修复程序 . 出于某种原因,我能够将地址保存到与社交 Profiles (Facebook,Twitter等)不统一的联系人 . 但是,当我尝试为与Facebook或Twitter统一的联系人添加地址时,我收到一个奇怪的保存错误:
The operation couldn’t be completed. (CNErrorDomain error 500.)
以下是我正在使用的一些代码:
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 回答
我理解Apple为什么要这样做,因为社交帐户不存储与(比如说)iCloud联系人相同的所有数据,因此您需要将这些值保存在其他地方,(并且您无法将联系人转换为iCloud作为社交帐户将丢失其数据 . )
我不认为您可以选择以编程方式链接联系人,不过,可以吗?
据我所知,iOS将决定两个联系人是否相同 - 可能是多个值匹配时(?)您可以检测两个给定的联系人是否链接在一起 .
好的,所以我得到了Apple的回复,这个行为是有意的 . 开发人员应检测此违反策略,然后提供创建新联系人,然后链接两个联系人 .