CNContactPickerViewController [CNUI ERROR] 错误调用服务 - 无法与帮助应用程序通信

Posted

技术标签:

【中文标题】CNContactPickerViewController [CNUI ERROR] 错误调用服务 - 无法与帮助应用程序通信【英文标题】:CNContactPickerViewController [CNUI ERROR] error calling service - Couldn’t communicate with a helper application 【发布时间】: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

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

【问题讨论】:

希望对您有所帮助! developer.apple.com/videos/play/wwdc2015/223。您可以从此链接找到所有需要的信息。 【参考方案1】:

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

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

【讨论】:

以上是关于CNContactPickerViewController [CNUI ERROR] 错误调用服务 - 无法与帮助应用程序通信的主要内容,如果未能解决你的问题,请参考以下文章