I want to get the country code and phone number from CNContact on iOS 9. I tried many things but couldn't find a way. The best result I achieved is printing:
<CNPhoneNumber: 0x7f886389a140: countryCode=us, digits=5555648583>
Here's how I do that:
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
print(contact.phoneNumbers.count)
for number: CNLabeledValue in contact.phoneNumbers {
print(number.value)
}
}
What I want is the values for countryCode and digits. Any ideas how to access them in Swift? Thanks!
Unfortunately you can't get them since they are private.
let numberValue = number.value
let countryCode = numberValue.valueForKey("countryCode") as? String
let digits = numberValue.valueForKey("digits") as? String
This works but if you do something in this lines your app will most likely be rejected.
You can see all the nice stuff you could use here.
If you don't plan on uploading your app to the store the solution above is OK, otherwise I'd stick with some kind of regex knowing it can break in the future:
countryCode=(\w{2}),.*digits=(.+)>$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With