iOS Swift CNContactPickerViewController搜索联系人并添加到选择

时间:2016-07-01 11:11:41

标签: ios search swift2 cncontact contactpicker

我正在使用iOS 9和Swift 2.2

我已使用CNContactPickerViewController内置CNContactPickerDelegate iOS来获取联系号码,

CNContactPickerViewController屏幕中,当我单击顶部的搜索字段并搜索名称时,我需要将该名称添加到我的选择中,但在点击联系人后没有任何反应。

我到处搜索,并找到任何解决方案

我是否需要在代码中添加任何内容,或者是iOS 9错误

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

4 个答案:

答案 0 :(得分:3)

搜索结果似乎仅在单选模式下有效,因此请确保实现

func contactPicker(CNContactPickerViewController, didSelect: CNContact)

仅,但不

func contactPicker(CNContactPickerViewController, didSelect: [CNContact])

如果同时实现这两种方法,则仅使用一个CNContact作为参数的版本,而将使用多选模式。

答案 1 :(得分: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 :(得分:1)

这是 swift 4 版本

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

答案 3 :(得分:0)

多重选择和搜索是互斥的。如果要使搜索有效,则必须仅使用单选,并且仅实现单选委托方法。