Skip to content

Instantly share code, notes, and snippets.

@arslanbilal
Last active April 4, 2023 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arslanbilal/b95e56fac36445dacee4131e11a7d6d0 to your computer and use it in GitHub Desktop.
Save arslanbilal/b95e56fac36445dacee4131e11a7d6d0 to your computer and use it in GitHub Desktop.
ContactsUI select contact property
import ContactsUI
class ExampleViewController: UIViewController {
private func dispayContacts() {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.displayedPropertyKeys = [CNContactGivenNameKey, CNContactPhoneNumbersKey]
contactPicker.predicateForEnablingContact = NSPredicate(format: "phoneNumber.@count > 0")
contactPicker.predicateForSelectionOfContact = NSPredicate(value: false)
contactPicker.predicateForSelectionOfProperty = NSPredicate(format: "key == 'phoneNumbers'")
self.present(contactPicker, animated: true)
}
}
extension ExampleViewController: : CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
if contactProperty.key == "phoneNumbers",
let phoneNumber = contactProperty.value as? CNPhoneNumber {
// contactProperty.contact (the selected contact)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment