如何在iOS中获取用户的国家/地区呼叫代码?

问题描述:

我正在开发一个用户输入手机号码的iOS应用。我如何获得他们的国家/地区呼叫代码?例如,如果用户在印度,则应自动为 +91 添加前缀。是否有自动添加国家/地区代码的选项?

I am developing an iOS app in which the user enters their mobile number. How do I get their country calling code? For example, if a user is in India, then +91 should be prefixed automatically. Is there an option that adds country codes automatically?

导入声明:

#import<CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

您可以使用CoreTelephony框架获取当前运营商的国家/地区代码:

you can get country code for the current Carrier using CoreTelephony framework:

CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = network_Info.subscriberCellularProvider;

NSLog(@"country code is: %@", carrier.mobileCountryCode);

//will return the actual country code
NSLog(@"ISO country code is: %@", carrier.isoCountryCode);

Apple Docs