获取当前国家时出错

时间:2012-07-26 05:32:04

标签: iphone locale

我使用以下代码获取当前国家/地区

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:countryCodes];
NSLog(@"%@",countryName);

我在阿联酋,但这显示美国,我在我的iPod上测试,请帮助我如何获得当前的国家?

1 个答案:

答案 0 :(得分:2)

-(void)viewWillAppear:(BOOL)animated
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [super viewWillAppear:animated];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    NSLog(@"Detail is:--> %@",placemark);

    NSString *strCountry = [NSString stringWithFormat:@"Current Cuntry %@",[placemark.addressDictionary objectForKey:@"Country"]];

    NSLog(@"label detail is:--> %@",strCountry);

    lblAddress.text = strCountry;
}

试试此代码......这肯定会为您提供当前的国家/地区详细信息。