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=(.+)>$
Objective-C:
[number.value valueForKey:@"countryCode"]
Swift:
number.value.valueForKey("countryCode") as? String
valueForKey
is not private, and your app will not get rejected.
They two members can be accessed via valueForKey
:
let countryCode = number.valueForKey("countryCode") as? String
let digits = number.valueForKey("digits") as? String
Please note that due to the fact that these two fields are part of a private API, there's no guarantee that in the future versions of the Contacts
framework they won't be removed/replaced.
/* Get only first mobile number */
let MobNumVar = (contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String
print(MobNumVar)
/* Get all mobile number */
for ContctNumVar: CNLabeledValue in contact.phoneNumbers
{
let MobNumVar = (ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String
print(MobNumVar!)
}
/* Get mobile number with mobile country code */
for ContctNumVar: CNLabeledValue in contact.phoneNumbers
{
let FulMobNumVar = ContctNumVar.value as! CNPhoneNumber
let MccNamVar = FulMobNumVar.valueForKey("countryCode") as? String
let MobNumVar = FulMobNumVar.valueForKey("digits") as? String
print(MccNamVar!)
print(MobNumVar!)
}
fetch contacts without country code, spaces ,braces and dash from contacts book with contact picker and textfield both
*********** without country code
import ContactsUI
var phoneString:String!
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
let numbers = contact.phoneNumbers.first
let a = (numbers?.value)?.stringValue ?? ""
let myString = a
let formattedString = myString.replacingOccurrences(of: " ", with: "")
let newFormattedString = formattedString.replacingOccurrences(of: "(", with: "")
let formatstring = newFormattedString.replacingOccurrences(of: ")", with: "")
let last10 = formatstring.replacingOccurrences(of: "-", with: "")
phoneString = String(last10.suffix(10))
phonetextField.text = phoneString
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
self.dismiss(animated: true, completion: nil)
}
@IBAction func inviteButton(_ sender : Any)
{
if phoneString == nil{
phoneString = phonetextField.text! //fetching from phonetextfield
Phone = phoneString
}
else {
Phone = phoneString //fetching from phone contacts
}
}
To get Country code you can use this:
(contact.phoneNumbers[0].value ).value(forKey: "countryCode") as! String
in for loop, and key; "digits" is to get full phone number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.