Reputation: 559
I am using a iphone5s which i bought from US and using the same in India for development also using the indian carrier. I am trying to get the country code with NSLocale
but this gives me the US
, instead of IN
.
What should i do to make it IN
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"country code %@",countryCode); //US
Upvotes: 1
Views: 3862
Reputation: 3198
You have to Follow below method
in Appdelegate.h file
//#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);
}
}];
}
Upvotes: 0
Reputation: 1491
In Swift 3:
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
Upvotes: 1
Reputation: 35686
NSLocale
's currentLocale
will give you information about the locale set on the device settings (Language & Region).
If you want to get the country code of the carrier instead, you'll have to use CoreTelephony
framework:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
...
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
A couple of things to watch for though:
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.
Upvotes: 8