Getting contacts from address book
info.plist:
Privacy - Contacts Usage Description
Import ContactsUI
inherit from: CNContactPickerDelegate
In button click:
let cnPicker = CNContactPickerViewController()
cnPicker.delegate = self
self.present(cnPicker, animated: true, completion: nil)
Implement function:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let userName:String = contact.givenName
// user phone number
let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value
// user phone number string
let primaryPhoneNumberStr:String = firstPhoneNumber.stringValue
print(primaryPhoneNumberStr)
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("Cancel Contact Picker")
}
Privacy - Contacts Usage Description
Import ContactsUI
inherit from: CNContactPickerDelegate
In button click:
let cnPicker = CNContactPickerViewController()
cnPicker.delegate = self
self.present(cnPicker, animated: true, completion: nil)
Implement function:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let userName:String = contact.givenName
// user phone number
let userPhoneNumbers:[CNLabeledValue<CNPhoneNumber>] = contact.phoneNumbers
let firstPhoneNumber:CNPhoneNumber = userPhoneNumbers[0].value
// user phone number string
let primaryPhoneNumberStr:String = firstPhoneNumber.stringValue
print(primaryPhoneNumberStr)
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("Cancel Contact Picker")
}
Comments
Post a Comment