在尝试更新现有联系人时,我收到 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 联系人内部发生的事情!希望能帮到你,抱歉没有早点给你回复。

基本上,您可以将接触循环内部的内容更改为
let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566")

cntct.phoneNumbers.append(phone)

你应该有一个电话号码添加过程。

关于swift - 附加电话号码时联系框架崩溃的应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35090828/

10-13 03:06