简体   繁体   English

iOS如何查找用户电话号码的国家代码?

[英]iOS how to find country code of the user's phone number?

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. 我找到了多个答案,声称使用核心电话你可以得到国家代码“+1”,但我尝试了一些,但没有一个工作。

I tried this: 我试过这个:

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);

which prints: 打印:

"country code is: 310, mnc: 410, mcc: 310"

So, for a US number I would like to get +1. 所以,对于一个美国号码,我想获得+1。

Is there a way to do this? 有没有办法做到这一点?

EDIT: With further search I found this: 编辑:进一步搜索我发现:

How to map MCC + MNC to users phone country code in iOS objective-C without using Location info? 如何在不使用位置信息的情况下将MCC + MNC映射到iOS objective-C中的用户手机国家/地区代码?

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];

Which will give you US for a USA based device. 这将为您提供US的美国设备。 You can map from country code to country phone code via the JSON data available on this . 您可以从国家代码通过上可用的JSON数据映射到国家电话区号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM