iOS 获取国家代码不起作用

iOS Get Country Code not working

我正在使用我从美国购买的 iphone5s,并在印度使用它进行开发,同时使用印度运营商。我正在尝试使用 NSLocale 获取国家代码,但这给了我 US,而不是 IN

我应该怎么做才能做到IN

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"country code %@",countryCode);  //US 

NSLocalecurrentLocale 将为您提供有关设备设置(语言和地区)上设置的区域设置的信息。

如果您想获取运营商的国家代码,则必须使用 CoreTelephony 框架:

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

...

CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;

不过有几点需要注意:

The value for this property (isoCountryCode) is nil if any of the following apply:

  • The device is in Airplane mode.

  • There is no SIM card in the device.

  • The device is outside of cellular service range.

More info on the docs here

获取国家代码

您必须遵循以下方法

在Appdelegate.h文件中

//#import CoreLocation/CoreLocation.h>

@interface AppDelegate : UIResponder  UIApplicationDelegate,CLLocationManagerDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self getCurrentLocation];
}

#pragma mark - CLLocatin delegate && Location Methdos

-(void)getCurrentLocation {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // To calculate loaction on 500 meters
    
    /* CLLocationDistance kilometers = 0.5 1000.0; //user will be notified when distance is changed by 40km from current distance
    locationManager.distanceFilter = kilometers; */
#ifdef __IPHONE_8_0
    if (IS_OS_8_OR_LATER)
    {
        [locationManager requestAlwaysAuthorization];
    }
#endif
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
        [locationManager stopUpdatingLocation];
        [self getCurrentCountry];
    }
}

- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError *)anError {
    switch([anError code])
    {
        case kCLErrorNetwork: // general, network-related error
        {
        }
            break;
        case kCLErrorDenied:{
        }
            break;
        default:
        {
        }
            break;
    }
}

-(void)getCurrentCountry {
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:locationManager.location
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                        if (error == nil && [placemarks count] > 0)
                        {
                            NSLog(@"Current country: %@", [[placemarks objectAtIndex:0] country]);
                            NSLog(@"Current country code: %@", [[placemarks objectAtIndex:0] ISOcountryCode]);
                            
                            NSLog(@"CountryCode=%@",GetContryCode);

                            SetContryCode
                            setBoolForCountryCode(YES);
                            NSLog(@"CountryCode=%@",GetContryCode);
                        }
                   }];
}

在Swift 3:

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
        print(countryCode)
}