当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - CNContactPickerViewController [CNUI ERROR] error calling service - Couldn’t communicate with a helper application

问题描述:

I'm using below code to let the user select contacts from their saved contacts:

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)

}

}

}

The first time it works perfectly. But the second time, when this code is executed

if !selectecPhoneBookContacts.isEmpty{

contactPicker.predicateForEnablingContact = CNContact.predicateForContacts(withIdentifiers: selectecPhoneBookContacts)

}

I end up in errors:

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

What I wanted to is: let the user only select a contact once. If the contact is selected once, it can't be added a second time

网友答案:

I solved this by using my own NSPredicate:

if !selectecPhoneBookContacts.isEmpty{
  contactPicker.predicateForEnablingContact = NSPredicate(format: "NOT(identifier IN %@)", selectecPhoneBookContacts)
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: