如何在 iPhone xcode 上检测 GPS 开/关,我们如何知道我们的应用程序不允许使用 gps?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6854284/
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
How do detect GPS on/of at iPhone xcode and how can we know about our application isn't allowed using gps?
提问by user4951
I want to make application that will use GPS, as we know at first time user use our application that need gps/current location, there is a pop up that asking for permission, the problem is.. example:
我想制作将使用 GPS 的应用程序,因为我们知道用户第一次使用需要 gps/当前位置的应用程序时,会弹出一个请求许可的窗口,问题是.. 示例:
the user choose not allow, and then how can we know the user make our application can't access to her/his gps to know his/her location? because my application need CurrentLocation, so if I can detect what user choose, I want to make a Pop Up like first Pop Up that asking for permission again.
用户选择不允许,然后我们怎么知道用户让我们的应用程序无法访问她/他的 GPS 来知道他/她的位置?因为我的应用程序需要 CurrentLocation,所以如果我可以检测到用户选择了什么,我想制作一个像第一个弹出窗口一样再次请求许可的弹出窗口。
or any code that can make gps turn on/off by my application?
或者任何可以通过我的应用程序打开/关闭 GPS 的代码?
Imagine if the user does not allow by mistake? Is there away for my application to reask the user?
试想一下,如果用户错误地不允许?我的应用程序是否可以重新询问用户?
What should application like Yelp do when location is not available?
当位置不可用时,像 Yelp 这样的应用程序应该怎么做?
回答by Noah Witherspoon
As of iOS 4.2, the class on which you're implementing CLLocationManagerDelegate's methods (like -locationManager:didUpdateToLocation:fromLocation:
) should also implement -locationManager:didChangeAuthorizationStatus:
. There are four statuses that that method will receive; to check for your app being unable to use location services, look for kCLAuthorizationStatusRestricted
—when the user is unable to allow access to location services—and kCLAuthorizationStatusDenied
—when the user has explicitly refused your application access to location services. In both cases, the appropriate thing to do is to inform the user (via an alert view or whatever) that your application relies on being able to access their location and that they may be able to re-grant it that access in the Settings app. You can also check your app's authorization status at any time using the CLLocationManager class method +authorizationStatus
.
从 iOS 4.2 开始,您在其上实现 CLLocationManagerDelegate 方法(如-locationManager:didUpdateToLocation:fromLocation:
)的类也应该实现-locationManager:didChangeAuthorizationStatus:
. 该方法将收到四种状态;要检查您的应用程序是否无法使用位置服务,请查找kCLAuthorizationStatusRestricted
- 当用户无法允许访问位置服务时 - 以及kCLAuthorizationStatusDenied
- 用户明确拒绝您的应用程序访问位置服务的时间。在这两种情况下,正确的做法是通知用户(通过警报视图或其他方式)您的应用程序依赖于能够访问他们的位置,并且他们可能能够在“设置”应用程序中重新授予该访问权限. 您还可以随时使用 CLLocationManager 类方法检查应用程序的授权状态+authorizationStatus
。
Pre-4.2, unfortunately, none of that is available, and you'll need to use the +locationServicesEnabled
method that sosbom's answer mentions.
不幸的是,在 4.2 之前,这些都不可用,您需要使用+locationServicesEnabled
sosbom 的答案中提到的方法。
回答by sosborn
Read this: Location Awareness Programming
阅读本文:位置感知编程
The key line is this:
关键是这样的:
Determining Whether Location Services Are Available Every iOS-based device is capable of supporting location services in some form but there are still situations where location services may not be available:
The user can disable location services in the Settings application. The user can deny location services for a specific application. The device might be in Airplane mode and unable to power up the necessary hardware. For these reasons, it is recommended that you always call the locationServicesEnabled class method of CLLocationManager before attempting to start either the standard or significant-change location services. (In iOS 3.x and earlier, check the value of the locationServicesEnabled property instead.) If this class method returns YES, you can start location services as planned. If it returns NO and you attempt to start location services anyway, the system prompts the user to confirm whether location services should be reenabled. Given that location services are very likely to be disabled on purpose, the user might not welcome this prompt.
确定定位服务是否可用每个基于 iOS 的设备都能够以某种形式支持定位服务,但仍然存在定位服务可能不可用的情况:
用户可以在设置应用程序中禁用定位服务。用户可以拒绝特定应用程序的定位服务。设备可能处于飞行模式,无法启动必要的硬件。由于这些原因,建议您始终在尝试启动标准或重大更改位置服务之前调用 CLLocationManager 的 locationServicesEnabled 类方法。(在 iOS 3.x 及更早版本中,改为检查 locationServicesEnabled 属性的值。)如果此类方法返回 YES,则您可以按计划启动位置服务。如果返回 NO 并且您仍然尝试启动定位服务,系统会提示用户确认是否应重新启用定位服务。鉴于定位服务很可能被故意禁用,