ios 从 iPhone 获取时区/国家

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19186666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:05:02  来源:igfitidea点击:

Get Timezone/country from iPhone

iphoneiostimezone

提问by Alan

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!

谢谢!

回答by Tom Harrington

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.

名称将类似于Australia/Sydney, or Europe/Zurich, or America/Denver。由于听起来您可能只关心大陆,这可能就是您所需要的。

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.gzis when you're reading this). There's a file in that archive named zone.tabthat gives a lat/long for each time zone, for example:

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

CH      +4723+00832     Europe/Zurich

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

+4723+00832表示纬度= + 47o23" ,经度= + 8o23" 。

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

更新:在某些情况下(我不知道)里,时区名称可能是一个老式的名称,比如US/Pacific未上市zone.tab。要处理这些问题,还需要获取名为 的文件backward,该文件将旧名称翻译为当前名称。

Converting zone.tabinto 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:

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

  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.

  1. 用户在“设置”中更改他们的时区。大多数用户让他们的手机自动设置时间和时区,但不是每个人。如果用户更改他们的时区,您将获得他们想要的任何内容,而不是准确的内容。

  2. 设备无法确定正确的本地时区。网络问题可能会阻止手机准确设置时间和时区。例如,如果用户乘飞机旅行并且在降落时出现网络问题,手机可能仍会显示从他们的出发地点开始的时区。

回答by Pradeep Mahdevu

Update: I am changing my answer after the last comment.

更新:我在最后一条评论后更改我的答案。

You can reverse lookup for IP of the phone to find out geo location. There are several rest APIs available.

您可以反向查找电话的 IP 以找出地理位置。有几个可用的其余 API

Saying that I don't recommend this as this may cause Apple to reject your application. Safe bet is to go thru Apple-provided location services

说我不推荐这样做,因为这可能会导致 Apple 拒绝您的申请。安全的赌注是通过 Apple 提供的定位服务

回答by Prosenjit Goswami

    NSTimeZone *timeZoneLocal = [NSTimeZone localTimeZone];
    NSString *strTimeZoneName = [timeZoneLocal name];

    or

    NSTimeZone *timeZoneLocal = [NSTimeZone defaultTimeZone];
    NSString *strTimeZoneName = [timeZoneLocal name];