iOS Swift CNContactPickerViewController search contact and add to selection
Asked Answered
P

4

19

I am using iOS 9 and Swift 2.2

I have implemented iOS inbuilt CNContactPickerViewController using CNContactPickerDelegate to get the contact numbers,

In the CNContactPickerViewController Screen, when I click on search field on top and search for a name, I need to add that name to my selection but nothing happens after tapping the contact.

I searched everywhere and dint find any solution to this

Do I need to add anything to my code or is it a iOS 9 bug

@IBAction func AddBtnKlkFnc(sender: AnyObject)
{
    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self
    contactPicker.displayedPropertyKeys =
        [CNContactPhoneNumbersKey]
    self.presentViewController(contactPicker, animated: true, completion: nil)
}

func contactPicker(picker: CNContactPickerViewController, didSelectContacts ContctAryVar: [CNContact])
{
    for ContctVar in ContctAryVar
    {
        let ContctDtlVar = ContctDtlCls()
        ContctDtlVar.ManNamVar = CNContactFormatter.stringFromContact(ContctVar, style: .FullName)!

        for ContctNumVar: CNLabeledValue in ContctVar.phoneNumbers
        {
            var MobNumVar  = ((ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String)!
            if(MobNumVar.Len() > 10)
            {
                MobNumVar = MobNumVar.GetLstSubSrgFnc(10)
            }
            ContctDtlVar.MobNumVar = MobNumVar
            ContctDtlAryVar.append(ContctDtlVar)
        }
    }
}
Platonic answered 1/7, 2016 at 11:11 Comment(5)
That is a known issue: see openradar.me/25433471.Atrocious
I just want to confirm that this has not been fixed in iOS 10.Inelegancy
Nop It has not been fixed even in iOS 10.2Platonic
Still not fixed in iOS 10.3.1 !!!Citrin
Hii Sujay. Did you solve that issue? I am facing the same issue.Hilariohilarious
F
2
Use this updated code and  


   @IBAction func AddBtnKlkFnc(sender: AnyObject)
    {
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        contactPicker.displayedPropertyKeys =
            [CNContactPhoneNumbersKey]
        self.presentViewController(contactPicker, animated: true, completion: nil)
    }

    func contactPicker(picker: CNContactPickerViewController, didSelectContacts ContctAryVar: [CNContact])
    {
        for ContctVar in ContctAryVar
        {
            let ContctDtlVar = ContctDtlCls()
            ContctDtlVar.ManNamVar = CNContactFormatter.stringFromContact(ContctVar, style: .FullName)!

            for ContctNumVar: CNLabeledValue in ContctVar.phoneNumbers
            {
                var MobNumVar  = ((ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String)!
                if(MobNumVar.Len() > 10)
                {
                    MobNumVar = MobNumVar.GetLstSubSrgFnc(10)
                }
                ContctDtlVar.MobNumVar = MobNumVar
                ContctDtlAryVar.append(ContctDtlVar)
            }
        }

     delegate.didFetchContacts([contact])
    navigationController?.popViewControllerAnimated(true)
    }
Flavone answered 10/7, 2017 at 7:3 Comment(1)
here wat is a delegate. i am getting this "Use of unresolved identifier 'delegate'" how to slove thatHilariohilarious
G
12

The search results seem to be working in single selection mode only, so make sure you implement

func contactPicker(CNContactPickerViewController, didSelect: CNContact)

only, but not

func contactPicker(CNContactPickerViewController, didSelect: [CNContact])

If you implement both, the version wich takes only one CNContact as argument is ignored and the multi selection mode is used instead.

Gambill answered 17/11, 2018 at 18:21 Comment(3)
Brilliant! I would like to add 100 to this response thank you very much @GambillLarrisa
It isn't working for me, delegate method func contactPicker(CNContactPickerViewController, didSelect: CNContact) never gets called while selection after searchGrad
@iOSMonster did you setup your delegate correctly? maybe ask a new question and show your codeGambill
F
2
Use this updated code and  


   @IBAction func AddBtnKlkFnc(sender: AnyObject)
    {
        let contactPicker = CNContactPickerViewController()
        contactPicker.delegate = self
        contactPicker.displayedPropertyKeys =
            [CNContactPhoneNumbersKey]
        self.presentViewController(contactPicker, animated: true, completion: nil)
    }

    func contactPicker(picker: CNContactPickerViewController, didSelectContacts ContctAryVar: [CNContact])
    {
        for ContctVar in ContctAryVar
        {
            let ContctDtlVar = ContctDtlCls()
            ContctDtlVar.ManNamVar = CNContactFormatter.stringFromContact(ContctVar, style: .FullName)!

            for ContctNumVar: CNLabeledValue in ContctVar.phoneNumbers
            {
                var MobNumVar  = ((ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String)!
                if(MobNumVar.Len() > 10)
                {
                    MobNumVar = MobNumVar.GetLstSubSrgFnc(10)
                }
                ContctDtlVar.MobNumVar = MobNumVar
                ContctDtlAryVar.append(ContctDtlVar)
            }
        }

     delegate.didFetchContacts([contact])
    navigationController?.popViewControllerAnimated(true)
    }
Flavone answered 10/7, 2017 at 7:3 Comment(1)
here wat is a delegate. i am getting this "Use of unresolved identifier 'delegate'" how to slove thatHilariohilarious
F
2

Here is a swift 4 version

@IBAction func addPhoneContact(_ sender: UIButton) {
    let contactPicker = CNContactPickerViewController()
    contactPicker.delegate = self
    contactPicker.displayedPropertyKeys =
        [CNContactPhoneNumbersKey]
    self.present(contactPicker, animated: true, completion: nil)
}

extension ViewController: CNContactPickerDelegate {
  func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
      picker.dismiss(animated: true, completion: nil)
      let name = CNContactFormatter.string(from: contact, style: .fullName)
      for number in contact.phoneNumbers {
        let mobile = number.value.value(forKey: "digits") as? String
        if (mobile?.count)! > 7 {
            // your code goes here
        }
     }
  }
}
Fluviomarine answered 11/6, 2018 at 6:56 Comment(0)
K
2

Multi selection and search are mutually exclusive. If you want search to be working you have to go with single selection only and implement only single selection delegate method.

Knapweed answered 13/11, 2019 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.