How to contacts with checkbox when user search the contact in search bar in swift

135 views Asked by At

I want to search the contacts through the search bar and check those contacts with a checkbox by using the contact picker in swift. so I tried it, I search the contact and it only shows the name without checkbox for selecting the contacts. This is my code :

       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)")


            }

        }

So how to select the contacts along with checkbox through the search bar.

0

There are 0 answers