问题描述
我有一个应用需要找出用户拥有的电话号码的国家/地区代码。我的理解是,我不能只获取用户的电话号码,但是,有没有办法,例如,如果我有一个美国电话号码来获取国家代码+1?
I have an app that needs to figure out the country code of the phone number the user has. My understanding is that I can't just get the phone number of the user, but, is there a way for example if I have a US phone number to get the country code +1?
我找到了多个答案声称使用核心电话可以获得国家代码+1,但我尝试了一些,但没有一个工作。
I have found multiple answers that claim that using core telephony you can get the country code "+1" but I tried a few and none of them work.
我试过这个:
CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = network_Info.subscriberCellularProvider;
NSString *mnc = [carrier mobileNetworkCode];
NSString *mcc = [carrier mobileCountryCode];
NSLog(@"country code is: %@, mnc: %@, mcc: %@", carrier.mobileCountryCode, mnc, mcc);
打印:
"country code is: 310, mnc: 410, mcc: 310"
所以,对于一个美国号码,我想获得+1。
So, for a US number I would like to get +1.
有没有办法做到这一点?
Is there a way to do this?
编辑:
进一步搜索我发现了这个:
With further search I found this:
它可能会成功,但我正在寻找一种更正式的方式来做到这一点。
It might do the trick, but I was looking for a more official way to do this.
推荐答案
您可以通过以下方式获取国家/地区代码:
You can get the country code with:
NSString *cc = [carrier isoCountryCode];
美国将为您提供美国
基于设备。您可以通过上提供的JSON数据,从国家/地区代码映射到国家/地区电话代码。
Which will give you US
for a USA based device. You can map from country code to country phone code via the JSON data available on this.
这篇关于iOS如何查找用户电话号码的国家代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!