Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

ios - Get Timezone/country from iPhone

Is there anyway to tell which timezone/country the phone is located in? I would prefer not to use location services.

The purpose of this is to automatically route calls. If the user is in the US, I want the app to default to calling the US based call center. If the user is in another country, I want it to default to their call center.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the name of the local time zone using:

NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];

The name will be something like Australia/Sydney, or Europe/Zurich, or America/Denver. Since it sounds like you might only care about the continent, that might be all you need.

If you need more: Get the official time zone database at http://www.iana.org/time-zones. Download tzdata2013g.tar.gz (or whatever the most recent version of tzdataYYYYx.tar.gz is when you're reading this). There's a file in that archive named zone.tab that gives a lat/long for each time zone, for example:

CH      +4723+00832     Europe/Zurich

The +4723+00832 indicates latitude = +47o23", longitude = +8o23".

Update: In some cases (I'm not sure which), the time zone name may be an older-style name like US/Pacific that isn't listed in zone.tab. To handle those, also get the file called backward, which gives translations of older names to current names.

Converting zone.tab into something your app can use is up to you. The results will be nowhere near as accurate as location services, but will give a general idea. But it's not guaranteed. It will fail in a couple of situations:

  1. The user changes their time zone in Settings. Most users let their phone set the time and zone automatically, but not everyone. If the user changes their time zone, you'll get whatever they want instead of what's accurate.

  2. The device is unable to determine the correct local time zone. Network problems might prevent the phone from setting the time and zone accurately. For example, if the user travels by plane and has network problems when they land, the phone may still show the time zone from their departure location.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...