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

g9icjywg  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(232)

我使用的是iOS 9和Swift 2.2

我已经实现了iOS内置的CNContactPickerViewController,使用CNContactPickerDelegate来获取联系人号码,
CNContactPickerViewController屏幕中,当我点击顶部的搜索字段并搜索一个名称时,我需要将该名称添加到我的选择中,但点击联系人后没有任何React。
我找遍了所有地方也没找到解决的办法
我是否需要在代码中添加任何内容,或者这是一个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)
        }
    }
}
bvjxkvbb

bvjxkvbb1#

搜索结果似乎只能在单选模式下工作,因此请确保实现

func contactPicker(CNContactPickerViewController, didSelect: CNContact)

仅,但不

func contactPicker(CNContactPickerViewController, didSelect: [CNContact])

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

5us2dqdw

5us2dqdw2#

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

vaqhlq813#

这是一个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
        }
     }
  }
}
t3psigkw

t3psigkw4#

多重选择和搜索是互斥的。如果你想搜索工作,你必须只进行单一选择,并只实现单一选择委托方法。

相关问题