更新现有的“通讯录”电话号码?

问题描述 投票:0回答:1

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

建议?

ios swift contacts phone-number
1个回答
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]
© www.soinside.com 2019 - 2024. All rights reserved.