【问题标题】:CNContactPickerViewController [CNUI ERROR] error calling service - Couldn’t communicate with a helper applicationCNContactPickerViewController [CNUI ERROR] 错误调用服务 - 无法与帮助应用程序通信
【发布时间】:2017-02-03 08:29:25
【问题描述】:

我正在使用下面的代码让用户从他们保存的联系人中选择联系人:

import UIKit
import ContactsUI

class AddGroupViewController:UIViewController{


  var contacts:[Contact] = []
  var selectecPhoneBookContacts:[String] = []

  override func viewDidLoad() {

  }

  @IBAction func selectPhoneBook(_ sender: Any) {
    let contactPicker = CNContactPickerViewController()
    contactPicker.displayedPropertyKeys = [
      CNContactGivenNameKey, CNContactFamilyNameKey
    ]
    contactPicker.delegate = self
    if !selectecPhoneBookContacts.isEmpty{
      contactPicker.predicateForEnablingContact = CNContact.predicateForContacts(withIdentifiers: selectecPhoneBookContacts)
    }
    self.present(contactPicker, animated: true, completion: nil)
  }

}

extension AddGroupViewController:CNContactPickerDelegate{
  func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
    for contact in contacts{
      selectecPhoneBookContacts.append(contact.identifier)
    }
  }
}

第一次完美运行。但是第二次,当这段代码被执行时

if !selectecPhoneBookContacts.isEmpty{
      contactPicker.predicateForEnablingContact = CNContact.predicateForContacts(withIdentifiers: selectecPhoneBookContacts)
    }

我最终会出错:

2017-02-03 09:23:37.501 SmartList[57062:10996092] viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)" UserInfo={Message=Service Connection Interrupted}
2017-02-03 09:23:37.504 SmartList[57062:10996203] [CNUI ERROR] error calling service - Couldn’t communicate with a helper application.
2017-02-03 09:23:47.479 SmartList[57062:10996092] [CNUI ERROR] Contact picker delayed appearance timed out - YES
2017-02-03 09:23:57.480 SmartList[57062:10996092] [CNUI ERROR] Contact picker delayed appearance timed out - YES

我想要的是:让用户只选择一次联系人。如果联系人被选中一次,则无法再次添加

【问题讨论】:

标签: ios swift


【解决方案1】:

我使用自己的 NSPredicate 解决了这个问题:

if !selectecPhoneBookContacts.isEmpty{
  contactPicker.predicateForEnablingContact = NSPredicate(format: "NOT(identifier IN %@)", selectecPhoneBookContacts)
}

【讨论】:

    猜你喜欢
    • 2018-03-31
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多