iOS 9:获取CNContact国家代码和电话号码 [英] iOS 9: get CNContact country code and phone number
问题描述
我想从iOS 9上的CNContact获取国家/地区代码和电话号码。我尝试过很多东西,但找不到办法。我实现的最佳结果是打印:
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>
以下是我的表现方式:
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
print(contact.phoneNumbers.count)
for number: CNLabeledValue in contact.phoneNumbers {
print(number.value)
}
}
我想要的是countryCode和digits的值。有任何想法如何在Swift中访问它们?谢谢!
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=(.+)>$
这篇关于iOS 9:获取CNContact国家代码和电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!