更新现有的'联系人'电话号码?

时间:2017-08-17 17:55:06

标签: ios swift contacts phone-number

我有代码从联系人中获取和显示用户数据(各种名称属性加上电话号码数组)。通过创建CNMutableContact,我可以更新任何Name属性。但我只能显示电话号码(来自CNLabeledValue通过" .digits")。是否也可以更新电话号码?我无法改变数字"在CNMutableContact中,因为它是“只读”#39;我已经看到并理解了访问CNLabeledValue"数字"的注意事项,但仍想继续。

建议?

1 个答案:

答案 0 :(得分:1)

回答这个问题可能有点晚了。但是,在给定的示例中,您只能添加电话号码。因此,要更新电话,充其量,您将需要替换删除电话号码并添加新电话号码。

用这种方法替换电话号码条目将导致更改电话号码的标识符

这不应该是您期望的行为,因为您正在更改现有条目的电话号码。而且我们希望保持电话标识符不变。

您应该使用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]