iOS Swift: Get user selected phone number from CNContactProperty as a string

2.5k Views Asked by At

As the title suggests, in my iOS app using swift I've got a CNContactProperty object and I want to extract the phone number from it as a string.

The CNContact property is returned from the standard CNContactPickerViewController via the delegate protocol function, after the user has selected a contact from it.

When a contact has multiple phone numbers, I want to be able to extract the one that the user tapped on in the contact view from the CNContactProperty.

I'm trying to do something like this:

 let myString = theCNContactProperty.value as! String

However, this crashes with an (lldb) error. I suspect that maybe the "value" property is not what I need?

I'm able to retrieve arbitrary numbers like so:

let myString = contactProperty.contact.phoneNumbers[0].value.stringValue

Which returns the first number a contact has. However, this doesn't serve my purpose as I want to be able to extract the specific number selected by the user when a contact has more than 1 number.

I've been working on this for hours and can't figure it out, I'd appreciate any help you can give me!

Edit: This is NOT a duplicate of the provided link. The linked question is about retrieving all numbers from a contact, NOT a specifically selected one. There is a HUGE difference in that.

Correct Answer:

As Mahdi Moqadasi wrote in the comments, the correct answer is to use (contactProperty.value as? CNPhoneNumber).stringValue.
Or see the following answers:
Extract email from CNContactProperty - iOS 9
iOS Objective C: Get user selected phone number from CNContactProperty as a string

2

There are 2 best solutions below

3
On

So you said that you want to be able to get the phone number the user selected from the CNContactViewController.

The CNContactViewController has a delegate function that returns the key the user selected. This is the function:

optional func contactViewController(_ viewController: CNContactViewController, 
  shouldPerformDefaultActionFor property: CNContactProperty) -> Bool

In that function, you can get the selected phone number by doing this:

let myString = property.identifier

Also, if you return false in this function, the action wont take place which I think means it wont automatically call the number.

4
On

You can use this - although, if the contact has more than one phone number, this will only get the first one...

var thePhoneLabel: String?
var thePhoneNumber: String?

func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {

    picker.dismissViewControllerAnimated(true, completion: {

        if contact.phoneNumbers.count > 0 {

            if let anEntry = contact.phoneNumbers.first {
                if let theNumber = anEntry.value as? CNPhoneNumber {

                    // Get the label for the phone number (Home, Work, Mobile, etc)
                    self.thePhoneLabel = CNLabeledValue.localizedStringForLabel(anEntry.label)

                    // Get the actual phone number (as a string)
                    self.thePhoneNumber = theNumber.stringValue

                }
            }

        } else {
            // contact has no phone numbers
            self.thePhoneLabel = "(No Phone)"
            self.thePhoneNumber = "(No Phone)"
        }

    })

}

EDIT:

If using:

contactPickerViewController.displayedPropertyKeys = [CNContactPhoneNumbersKey]

then:

var theContactName: String?
var thePhoneNumber: String?
var thePhoneLabel: String?

func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {

    theContactName = contactProperty.contact.givenName
    thePhoneNumber = contactProperty.value?.stringValue

    if let lbl = contactProperty.label {
        thePhoneLabel = CNLabeledValue.localizedStringForLabel(lbl)
    }

}

Note:

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

function will only be called if you do NOT have a

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact)