我想通过搜索栏搜索联系人,并使用 swift 中的联系人选择器使用复选框检查这些联系人。所以我试了一下,我搜索了联系人,它只显示了姓名,没有选择联系人的复选框。这是我的代码:
let cnPicker = CNContactPickerViewController()
cnPicker.delegate = self
self.present(cnPicker, animated: true, completion: nil)
do {
try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor])) {
(contact, cursor) -> Void in
self.res = contact
///let data = Data(name: contact.givenName)
//self.dataArray?.addObject(data)
}
}
catch
{
print("Handle the error please")
}
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
a.removeAll()
d.removeAll()
num.removeAll()
inProcess.removeAll()
for contact in contacts {
a+=[contact.givenName]
for ContctNumVar: CNLabeledValue in contact.phoneNumbers {
let FulMobNumVar = ContctNumVar.value
_ = FulMobNumVar.value(forKey: "countryCode") as? String
let MobNumVar = FulMobNumVar.value(forKey: "digits") as? String
num+=[MobNumVar!]
print("\(contact.givenName) phone number is:\( MobNumVar!)")
}
}
print(a)
print(num)
print(a.count)
let n = a.count
let res = Float(Float(amount)/Float(n))
print(res)
for _ in 1...n
{
d+=[(res)]
}
print(d)
for c in a {
print("selected contacts:\(c):\(res)")
}
}
那么如何通过搜索栏选择联系人以及复选框。