简体   繁体   English

从iPhone获取时区/国家/地区

[英]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! 谢谢!

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 . 这个名字将类似于Australia/SydneyEurope/Zurich ,或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 . 如果您需要更多:请访问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). 下载tzdata2013g.tar.gz (或者当您阅读本文时,最新版本的tzdataYYYYx.tar.gz )。 There's a file in that archive named zone.tab that gives a lat/long for each time zone, for example: 该档案中有一个名为zone.tab的文件,为每个时区提供一个纬度/经度,例如:

CH      +4723+00832     Europe/Zurich

The +4723+00832 indicates latitude = +47º23", longitude = +8º23". +4723+00832表示纬度= +47º23“,经度= +8º23”。

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 . 更新 :在某些情况下(我不确定哪个),时区名称可能是旧的名称,如US/Pacific ,未在zone.tab列出。 To handle those, also get the file called backward , which gives translations of older names to current names. 要处理这些,还要获取名为backward的文件,它将旧名称的翻译转换为当前名称。

Converting zone.tab into something your app can use is up to you. zone.tab转换为您的应用可以使用的内容取决于您。 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. 例如,如果用户乘飞机旅行并且在他们降落时遇到网络问题,则电话仍然可以从他们的出发地点显示时区。

Update: I am changing my answer after the last comment. 更新:我在最后一条评论后改变了我的答案。

You can reverse lookup for IP of the phone to find out geo location. 您可以反向查找手机的IP以查找地理位置。 There are several rest APIs available . 几个其他API可用

Saying that I don't recommend this as this may cause Apple to reject your application. 说我不推荐这个,因为这可能会导致Apple拒绝您的申请。 Safe bet is to go thru Apple-provided location services 安全的赌注是通过Apple提供的位置服务

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

    or

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM