从 iPhone 获取时区/国家 [英] Get Timezone/country from iPhone

查看:23
本文介绍了从 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.

谢谢!

推荐答案

您可以使用以下方法获取本地时区的名称:

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

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

名称将类似于 Australia/SydneyEurope/ZurichAmerica/Denver.既然听起来你可能只关心大陆,那可能就是你所需要的.

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.

如果您需要更多信息:在 http://www.iana.org/获取官方时区数据库时区.下载 tzdata2013g.tar.gz (或您阅读本文时的最新版本的 tzdataYYYYx.tar.gz ).该存档中有一个名为 zone.tab 的文件,它为每个时区提供了纬度/经度,例如:

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

+4723+00832表示纬度=+47º23",经度=+8º23".

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

更新:在某些情况下(我不确定是哪一种),时区名称可能是旧式名称,例如 US/Pacificzone.tab 中列出.要处理这些问题,还需要获取名为 backward 的文件,该文件将旧名称转换为当前名称.

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.

zone.tab 转换为您的应用可以使用的内容取决于您.结果远不如定位服务准确,但会给出一个大致的概念.但不能保证.它会在以下几种情况下失败:

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. 用户在设置"中更改时区.大多数用户让他们的手机自动设置时间和区域,但不是所有人.如果用户改变他们的时区,你会得到他们想要的,而不是准确的.

  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.

设备无法确定正确的本地时区.网络问题可能会导致手机无法准确设置时间和时区.例如,如果用户乘飞机旅行,降落时遇到网络问题,手机可能仍会显示其出发地的时区.

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.

这篇关于从 iPhone 获取时区/国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆