我正在开发一个应用程序,我在其中将联系人添加到联系人列表。我可以输入姓名、地址、电子邮件、电话、笔记等基本信息,但我想添加一些自定义字段,如 userLabel1、userValue1、bioPersonal、bioWork、bioOther。所以我想将自定义字段添加到地址 Book'contacts。
是否可以将自定义字段添加到联系人?如果是,那么请建议任何链接或示例代码?
基本上,没有办法做到这一点。地址簿不允许您添加自定义字段。但是,您可以将您的数据放在每个联系人的“备注”字段中。但是,这在您以外的应用程序中看起来很奇怪。
let store = CNContactStore()
// phoneNumberToEdit is the CNContact which you need to update
guard var mutableCont = phoneNumberToEdit?.mutableCopy() as? CNMutableContact else { return }
let phoneNumber = CNPhoneNumber(stringValue: "0123456789")
var currentContact = CNLabeledValue(label: "MyCustomLabel", value: phoneNumber)
mutableCont.phoneNumbers = [currentContact]
let request2 = CNSaveRequest()
request2.update(mutableCont)
do{
try store.execute(request2)
} catch let error{
print(error)
}