Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9: get CNContact country code and phone number

Tags:

ios

swift

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!

like image 624
Mr Stanev Avatar asked Jan 11 '16 20:01

Mr Stanev


1 Answers

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=(.+)>$
like image 140
fpg1503 Avatar answered Oct 06 '22 23:10

fpg1503