swiftcncontactcncontactpicker

Can't get the selected number from contactProperty


After spending many hours troubleshooting, searching and trying out some answers on StackOverFlow. I couldn't get this to work properly, I need to be able to select any number that a contact have, but I always get CNLabelPhoneNumberMain or in some cases CNLabelPhoneNumberMobile even when selecting home/work number.

I'm facing this trouble since long time and I appreciate if someone can point my mistake:

Here is my code:

@IBAction func addSupporterContactTapped(_ sender: UIButton) {

     let contactPicker = CNContactPickerViewController()
     contactPicker.displayedPropertyKeys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
     contactPicker.delegate = self
     self.present(contactPicker, animated: true, completion: nil)
}

And this is how selection is made:

func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {

    let numbers = contactProperty.contact.phoneNumbers
    var phoneNumber: String?

            for contact in numbers {
        switch contact.label {
        case CNLabelHome:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelWork:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelOther:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberMain:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberiPhone:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberMobile:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberPager:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberHomeFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberWorkFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        case CNLabelPhoneNumberOtherFax:
            print("contact.label = \(contact.label ?? "")")
            phoneNumber = (contact.value ).stringValue
        default:
            break
        }
    }

But in anyway, as I mentioned above, I never get the correctly selected number.

If it can help, When any number is selected, I get this printed in the console:

contact.label = _$!<Home>!$_
contact.label = _$!<Mobile>!$_
contact.label = _$!<HomeFAX>!$_

Is there anyone can help please?


Solution

  • When you select a phone number in picker: didSelect contactProperty: you get a CNPhoneNumber back in the value of contactProperty

    func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
        if let phoneNumberObject = contactProperty.value as? CNPhoneNumber {
            let phoneNumber = phoneNumberObject.stringValue
            print(contactProperty.label!, phoneNumber)
        }
    }