【问题标题】:Contact framework crashing app while appending phone number附加电话号码时联系人框架崩溃应用程序
【发布时间】:2016-05-07 13:27:11
【问题描述】:

在尝试更新现有联系人时,我收到 sigabrt 错误“获取联系人时未请求属性”。

var cntct= existingContact.mutableCopy() as! CNMutableContact

let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566") 

cntct.phoneNumbers.append(phone)

【问题讨论】:

标签: swift contacts


【解决方案1】:

我已经为自己的相关问题苦苦挣扎了几个小时:我需要为 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)

显然,这会添加生日而不是电话号码,但您可以使用完全相同的原则(谓词搜索,联系人中的联系人)来添加电话号码;只需更改联系人循环中的联系人内部发生的事情!希望对您有所帮助,抱歉没有早点回复您。

基本上,您可以将接触循环内的内容更改为

let phone= CNLabledValue(label:CNLabelPhoneNumberMain, value:"786967655566") 

cntct.phoneNumbers.append(phone)

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

【讨论】:

  • @Aslam 希望对您有所帮助;让我知道是否可以帮助您将其转换为添加电话号码!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2016-09-26
  • 2014-01-07
相关资源
最近更新 更多