Why is Locale.current.regionCode equal to zero?

I am trying to get the user region code in swift 3 using:

Locale.current.regionCode 

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

Thank you very much.

+7
source share
3 answers

I ran into the same problem, and as it turned out, it was because I tuned my circuit to a specific language. If you set both “Application Language and Application Region” to “System Language / Region”, it should work.

enter image description here

+5
source

Many Locale properties can return zero for various reasons on iOS (and Android).

You can also use objectForKey:NSLocaleCountryCode to request a country code.

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

It would be a good idea to have fallback logic for finding countryCode with CoreTelephony.

 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 example, in the Android version of the corresponding region query for Locale, this is nil for many root devices.

+1
source

Documentation for regions regionCode

Locale region code, or zero if none.

Some regions simply do not have a region (or country) code. However, I do not know which ones are not.

0
source

Source: https://habr.com/ru/post/1265113/


All Articles