iOS获取国家/地区代码无效

时间:2015-08-09 14:27:21

标签: ios iphone xcode nslocale

我正在使用iphone5s,我从美国购买并在印度使用相同的iphone5s进行开发也使用印度航空公司。我正在尝试使用NSLocale获取国家/地区代码,但这会给我US,而不是IN

我应该怎样做才能IN

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

3 个答案:

答案 0 :(得分:8)

****************************************************** Upgrade warning - for the CLI to run correctly, it is highly suggested to upgrade the following: Please update your Node runtime to version >=0.12.x ****************************************************** &#39; npm WARN engine cordova-js@4.0.0: wanted: {"node":"~0.10.x"} (current: {"node":"0.12.7","npm":"2.11.3"}) npm WARN engine npm@1.3.4: wanted: {"node":">=0.6","npm":"1"} (current: {"node":"0.12.7","npm":"2.11.3"}) npm WARN engine xmlbuilder@2.2.1: wanted: {"node":"0.8.x || 0.10.x"} (current: {"node":"0.12.7","npm":"2.11.3"}) npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "cordova" "ionic" npm ERR! node v0.12.7 npm ERR! npm v2.11.3 npm ERR! unexpected eof npm ERR! npm ERR! If you need help, you may report this error at: npm ERR! <https://github.com/npm/npm/issues> 将为您提供有关设备设置(语言和区域)上设置的区域设置的信息。

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

currentLocale

要注意的几件事情:

  

如果符合以下任何条件,则此属性的值(isoCountryCode)为nil:

     
      
  • 设备处于飞行模式。

  •   
  • 设备中没有SIM卡。

  •   
  • 该设备不在手机服务范围内。

  •   

More info on the docs here

答案 1 :(得分:1)

在Swift 3中:

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

答案 2 :(得分:0)

获取国家/地区代码

您必须遵循以下方法

在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);
                        }
                   }];
}