在尝试更新现有联系人时,我收到 sigabrt 错误“获取联系人时未请求属性”。
var cntct= existingContact.mutableCopy() as! CNMutableContact
let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566")
cntct.phoneNumbers.append(phone)
最佳答案
我已经为自己的相关问题苦苦挣扎了几个小时:我需要为与 swift 的联系人添加生日。经过一些研究、反复试验和故障排除,我得出以下结论:
var contactStore = CNContactStore()
var contactx:CNMutableContact = CNMutableContact()
let predicate = CNContact.predicateForContactsMatchingName("\(firstnamefield.text!) \(lastnamefield.text!) \(suffixfield.text!)") // searches for contacts matching the inserted name (inputted by the user as first name, then last name, then any suffixes).
let toFetch = [CNContactBirthdayKey]
do{
var contacts = try contactStore.unifiedContactsMatchingPredicate(
predicate, keysToFetch: toFetch)
print(contacts)
for contact in contacts {
let birthday = NSDateComponents()
birthday.year = Int(yearfield.text!)! // sets the birthday year
birthday.month = Int(monthfield.text!)! // sets the birthday month
birthday.day = Int(dayfield.text!)! // sets the birthday day
let mutableContact = contact.mutableCopy() as! CNMutableContact
mutableContact.birthday = birthday // sets the contacts found with predicate search to having the birthday set above.
let saveRequest = CNSaveRequest()
saveRequest.updateContact(mutableContact)
try contactStore.executeSaveRequest(saveRequest)
显然,这会添加生日而不是电话号码,但您可以使用完全相同的原则(谓词搜索,用于联系人中的联系人)来添加电话号码;只需更改 for contact in contacts 循环中发生的事情!我希望这对您有所帮助,很抱歉您之前没有得到回复。
基本上,您可以将接触环内部的内容更改为
let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566")
cntct.phoneNumbers.append(phone)
你应该有一个电话号码添加过程。
关于swift - 联系框架在附加电话号码时崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35090828/