【问题标题】:Update existing 'Contacts' Phone number?更新现有的“联系人”电话号码?
【发布时间】:2018-01-26 06:37:34
【问题描述】:
我有代码从联系人中获取和显示用户数据(各种名称属性加上电话号码数组)。通过创建 CNMutableContact,我可以更新任何 Name 属性。但我只能显示电话号码(来自 CNLabeledValue 通过“.digits”)。是否也可以更新电话号码?我无法更改 CNMutableContact 中的“数字”,因为它是“只读的”。我已经看到并理解访问 CNLabeledValue“数字”的注意事项,但仍想继续。
建议?
【问题讨论】:
-
您不使用 CNMutableContact.phoneNumbers 是否有特殊原因?看看this
-
标签:
ios
swift
contacts
phone-number
【解决方案1】:
也许回答这个问题有点晚了。但是,在给定的示例中,您只能添加电话号码。因此,要更新电话,您最多只能替换删除电话号码并添加新电话号码。
在这种方法中替换 phonenumber 条目会导致更改 phonenumber 的标识符。
这应该不是您所期望的行为,因为您正在更改现有条目的电话号码。我们希望保持电话标识符不变。
您应该使用 CNLabeledValue 中的 settingValue 请参阅: Apple Documentation
// Clone to a mutable contact
let mutableContact = contact.mutableCopy() as! CNMutableContact
// Here's the catch
let updatedPhone = mutableContact.phoneNumbers[0].settingValue(CNPhoneNumber(stringValue: "newPhone"))
// Be aware that in this example, I only have on phone number. For multiple phones, would need to handle multiple phones properly in the array.
mutableContact.phoneNumbers = [updatedPhone]