确定 iPhone 用户所在的国家/地区
对于 iPhone 应用程序,我需要在启动时确定用户位于哪个国家/地区。想必我将不得不打开位置服务并进行某种反向地理编码。如果可能的话,我真的不想使用第三方网络服务,是否有其他建议可以根据位置服务提供的内容确定国家/地区?
最初,我只需要检查用户是否在美国境内,但将来可能会改变以添加更多国家/地区。我了解位置无法始终确定,或者用户可能已关闭位置服务。基本上,我只需要知道用户是否被检测为在美国境内,以便关闭特定功能。
编辑:在进一步阅读时,看起来 MKReverseGeocoder 将是可行的方法,但我不希望在我的应用程序中显示任何地图,这意味着我不允许使用它。
I need to determine at startup what country a user is in, for an iPhone application. Presumably I will have to turn on location services and do some sort of reverse geocoding. I don't really want to use a third party web service if possible, are there any other suggestions for determining country from what location services provides?
Initially, I only need to check whether the user is inside the US or not, but this may change in the future to add more countries. I understand that location can not always be determined or the user may have location services turned off. Basically, I just need to know if the user is detected as being inside the US, in order to turn off a specific feature.
EDIT: On further reading, it looks like MKReverseGeocoder would be the way to go, except I don't wish to display any maps in my app, which means I'm not allowed to use this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试的另一个技巧是检查运营商的MCC:
Another trick you can try is checking the carrier’s MCC:
考虑到 MKReverseGeocoder 的限制,我看来实现我所追求的目标的唯一可行方法是使用第三方服务来执行反向地理编码。我选择使用 GeoNames 因为它们似乎是标准选择。
Given the restrictions of MKReverseGeocoder, it seems the only feasible way for me to achieve what I am after is to use a third party service to perform a reverse geocode. I have chosen to go with GeoNames as they seem to be the standard choice.
NSLocale 对象(例如由
[NSLocale systemLocale] 和
[NSLocale autoupdatingCurrentLocale]`返回)包含值 NSLocaleCountryCode。请在 Apple 文档中查看。The NSLocale object, such as returned by
[NSLocale systemLocale], and
[NSLocale autoupdatingCurrentLocale]`) contains the value NSLocaleCountryCode. Check it out in the Apple documentation.如果你不想使用iPhone SDK提供的Google服务,你不能只存储美国A边界的坐标并检查你是否在其中吗?
这是这种情况下的一个相关问题
如何确定 2D 点是否在多边形内?
如果目的限制的一部分不是用户体验(例如,强制遵守某些特定的美国法律),即当用户不能被信任时,我会说你需要一些更严格的检查(毕竟,用户会简单地否则禁止使用定位服务,不是吗?)。
其中一种方法是进行 IP 查找,例如 http://www.maxmind.com/app/ geoip_country
If you don't want to use the Google services provided by the iPhone SDK, couldn't you just store the coordinates of the US of A borders and check whether or not you are inside that?
Here is a relevant question in that case
How can I determine whether a 2D Point is within a Polygon?
If the purpose of the limitation is something other than user experience (for example, to enforce complicance with some specific US law), i.e. when the user can not be trused, I would say that you need some more rigorous checking (after all, the user would simply disallow the use of location services otherwise, wouldn't he/she?).
One such approach would be to do an IP lookup, e.g. http://www.maxmind.com/app/geoip_country