我使用以下代码让用户从已保存的联系人中选择联系人:
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
我想要的是:让用户只选择一次联系人。如果选择了一次联系人,则无法再次添加联系人
答案 0 :(得分:0)
我通过使用自己的NSPredicate来解决这个问题:
if !selectecPhoneBookContacts.isEmpty{
contactPicker.predicateForEnablingContact = NSPredicate(format: "NOT(identifier IN %@)", selectecPhoneBookContacts)
}