如何提示用户再次打开定位服务
我希望具有与地图应用程序相同的功能,每次用户按下“当前位置”按钮时都会提示用户打开其位置服务(如果它们已关闭):
- 关闭位置服务
- 用户按下“getCurrentLocation”按钮
- 应用程序尝试使用 CLLocationManager 获取位置
- 用户收到“打开定位服务...”消息,显示“设置”和“取消”按钮。
- 用户点击“取消”
- 用户再次按“getCurrentLocation”按钮
- 应用程序再次尝试使用 CLLocationManager 获取位置
- 用户执行不再收到“打开定位服务...”消息
在地图应用程序中,用户每次都会收到“打开定位服务...”消息。我怎样才能让我的应用程序执行同样的操作?我让用户使用 CLLocationManager 的新实例,以防出现问题,但事实并非如此。我看不到任何会影响此问题的设置。
如果我自己制作警报,我将无法获得相同的“设置”按钮功能。另外,我不希望用户看到多个看起来相同的警报。
有什么想法吗?
I want to have the same functionality as the Map app, where user is prompted every time they press the 'current location' button to turn on their Location Services if they are off:
- Turn off location services
- User presses 'getCurrentLocation' button
- App tries to get location using CLLocationManager
- User gets 'Turn On Location Services..." message that shows "Settings" and "Cancel" buttons.
- User taps 'Cancel'
- User presses ''getCurrentLocation' button again
- App tries to get location using CLLocationManager again
- User does not get 'Turn On Location Services..." message any more
In the Map app, the user gets "Turn On Location Services..." message every time. How can I get my app to do the same? I made user I am using a new instance of CLLocationManager, in case that was the problem, but it was not. I can't see any settings that would affect this.
If I make my own Alert I cannot get the same 'Settings' button functionality. Also, I don't want the user to see multiple Alerts that look the same.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iOS 8 中新增了一个名为
UIApplicationOpenSettingsURLString
的常量。来自“iOS 新增功能” 文档UIKit 下有这样一行:
来自Apple的 文档:
您可以将其传递到 UIApplication openURL: 方法中。它可能看起来像:
New in iOS 8 there is a constant called
UIApplicationOpenSettingsURLString
.From the "What's new in iOS" document under UIKit is the line:
From Apple's documentation:
You can pass this into the UIApplication openURL: method. It might look something like:
如果您想让用户返回“设置”应用中的“定位服务”屏幕,您可以通过将他们发送到一个特殊的 URL 来实现,如下所示:
If you want to point the user back to the Location Services screen in the Settings app, you can do so by sending them to a special URL, like so:
如果启用了位置服务,您可以查询共享的
CLLocationManager
实例。 正确的做法是尊重用户禁用位置服务的选择。但如果您愿意,只要启动定位服务,系统就会提示用户再次启动它。如果用户选择加入请求,您的代表将像往常一样开始报告位置。如果用户拒绝您的请求,您将收到
locationManager:didFailWithError:
委托方法的失败回调。该错误的错误代码为kCLErrorDenied
。我强烈建议您不要这样做,但如果用户拒绝,您可以尝试再次启动服务,系统将再次询问用户。但大多数用户会因此讨厌你。
You can query the shared
CLLocationManager
instance if the location service is enabled. The correct way is to respect the users choice to disable location services.But if you want to, just start the location service anyway and the user will be prompted to start it again. If the user opts in on the request locations will begin to be reported on your delegate as usual. If the user instead denies your request you will get a failure callback to the
locationManager:didFailWithError:
delegate method. The error will have an error code ofkCLErrorDenied
.I would strongly discourage you from doing this, but you can try to start the service again if the user says no, and the user will be asked again. Most users will hate you for it though.