iOS Swift CNContactPickerViewController search contact and add to selection

眉间皱痕 提交于 2019-11-29 11:31:57

问题


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

回答1:


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



回答2:


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.




回答3:


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
        }
     }
  }
}



回答4:


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.



来源:https://stackoverflow.com/questions/38143951/ios-swift-cncontactpickerviewcontroller-search-contact-and-add-to-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!