Here's an alternative, perhaps too circumventing method. Other solutions are based on manual settings (NSLocale) or when requesting permission to use location services that may be deprived (CLLocationManager), so they have disadvantages.
You can get the current country based on your local time zone. My application interacts with a server running Python with pytz installed, and this module provides a country code dictionary for timezone strings. I really need the server to know the country, so I donβt need to configure it completely on iOS. On the Python side:
>>> import pytz >>> for country, timezones in pytz.country_timezones.items(): ... print country, timezones ... BD ['Asia/Dhaka'] BE ['Europe/Brussels'] BF ['Africa/Ouagadougou'] BG ['Europe/Sofia'] BA ['Europe/Sarajevo'] BB ['America/Barbados'] WF ['Pacific/Wallis'] ...
On the iOS side:
NSTimeZone *tz = [NSTimeZone localTimeZone]; DLog(@"Local timezone: %@", tz.name);
I have a server that sends the name of the local time zone and looks it up in the pytz dictionary of country_timezones.
If you are creating an iOS dictionary version available in pytz or some other source, you can use it to immediately search for the country code without the help of a server, based on the time zone settings that are often relevant.
Maybe I'm wrong in NSLocale. Does it provide a country code with regional formatting options or time zone settings? If the latter, then this is just a more complicated way to get the same result ...
scott_at_skritter Jul 19 '12 at 23:23 2012-07-19 23:23
source share