为什么Locale.current.regionCode为零?

时间:2022-09-10 19:14:09

I am trying to get user's region code in swift 3 by using:

我试图通过使用以下方法在swift 3中获取用户的区域代码:

Locale.current.regionCode

Unfortunately regionCode is nil, do you have an idea why? And what should I do to obtain it?

不幸的是regionCode是零,你知道为什么吗?我应该怎么做才能获得它?

Thanks a lot.

非常感谢。

2 个解决方案

#1


4  

I ran into the same issue and as it turns out it was because I set my Scheme to a specific language. If you set both, Application Language & Application Region to "System Language / Region" it should work.

我遇到了同样的问题,事实证明这是因为我将我的Scheme设置为特定的语言。如果将“应用程序语言和应用程序区域”设置为“系统语言/区域”,它应该可以正常工作。

为什么Locale.current.regionCode为零?

#2


1  

Many of the properties on Locale can return nil for various reasons on iOS (and Android).

在iOS(和Android)上,由于各种原因,Locale上的许多属性都可以返回nil。

You may also use objectForKey:NSLocaleCountryCode to query the country code.

您还可以使用objectForKey:NSLocaleCountryCode来查询国家/地区代码。

// code may be nil
NSString *code = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; 

It's a good idea to have a fallback logic to find countryCode with CoreTelephony.

有一个回退逻辑,用CoreTelephony查找countryCode是个好主意。

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

Or/And with reverse geocode:

或者/和反向地理编码:

// CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    __block CLLocation *location = [locations firstObject];

    [[[CLGeocoder alloc] init] reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (!error && placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks firstObject];
            // Use placemark.country;
            // Use placemark.ISOCountryCode;
        }
    }
}

For instance, on Android version of corresponding region query for Locale, it's nil for many of the rooted devices.

例如,在针对Locale的相应区域查询的Android版本上,对于许多有根设备来说它是零。

#1


4  

I ran into the same issue and as it turns out it was because I set my Scheme to a specific language. If you set both, Application Language & Application Region to "System Language / Region" it should work.

我遇到了同样的问题,事实证明这是因为我将我的Scheme设置为特定的语言。如果将“应用程序语言和应用程序区域”设置为“系统语言/区域”,它应该可以正常工作。

为什么Locale.current.regionCode为零?

#2


1  

Many of the properties on Locale can return nil for various reasons on iOS (and Android).

在iOS(和Android)上,由于各种原因,Locale上的许多属性都可以返回nil。

You may also use objectForKey:NSLocaleCountryCode to query the country code.

您还可以使用objectForKey:NSLocaleCountryCode来查询国家/地区代码。

// code may be nil
NSString *code = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]; 

It's a good idea to have a fallback logic to find countryCode with CoreTelephony.

有一个回退逻辑,用CoreTelephony查找countryCode是个好主意。

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

Or/And with reverse geocode:

或者/和反向地理编码:

// CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    __block CLLocation *location = [locations firstObject];

    [[[CLGeocoder alloc] init] reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (!error && placemarks.count > 0) {
            CLPlacemark *placemark = [placemarks firstObject];
            // Use placemark.country;
            // Use placemark.ISOCountryCode;
        }
    }
}

For instance, on Android version of corresponding region query for Locale, it's nil for many of the rooted devices.

例如,在针对Locale的相应区域查询的Android版本上,对于许多有根设备来说它是零。