This question already has an answer here:
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.
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:In that function, you can get the selected phone number by doing this:
Also, if you return false in this function, the action wont take place which I think means it wont automatically call the number.
You can use this - although, if the contact has more than one phone number, this will only get the first one...
EDIT:
If using:
then:
Note:
function will only be called if you do NOT have a