iOS Swift CNContactPickerViewController search contact and add to selection

后端 未结 4 1675
星月不相逢
星月不相逢 2020-12-31 04:02

I am using iOS 9 and Swift 2.2

I have implemented iOS inbuilt CNContactPickerViewController using CNContactPickerDelegate to get the cont

4条回答
  •  Happy的楠姐
    2020-12-31 05:01

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

提交回复
热议问题