简体   繁体   English

如何检查是否已启用定位服务以进行应用?

[英]How to check if Location service is turned on for application?

I have application which uses user's location. 我有使用用户位置的应用程序。 On start of app loading it asks user for location. 开始加载应用程序时,它会询问用户位置。 How can I check after that if user is accepted to share location in my application? 之后,我该如何检查是否接受用户共享我的应用程序中的位置?

From CLLocationManager Reference 来自CLLocationManager参考

Returns a Boolean value indicating whether location services are enabled on the device. 返回一个布尔值,指示是否在设备上启用了位置服务。

  • (BOOL)locationServicesEnabled Return Value YES if location services are enabled; (BOOL)locationServicesEnabled返回值如果启用了位置服务,则为YES;否则为false。 NO if they are not. 不,如果不是。

Discussion The user can enable or disable location services from the Settings application by toggling the Location Services switch in General. 讨论用户通常可以通过切换“位置服务”开关来从“设置”应用程序启用或禁用位置服务。

You should check the return value of this method before starting location updates to determine whether the user has location services enabled for the current device. 您应该在开始位置更新之前检查此方法的返回值,以确定用户是否为当前设备启用了位置服务。 If this method returns NO and you start location updates anyway, the Core Location framework prompts the user to confirm whether location services should be reenabled 如果此方法返回“否”并且无论如何您都开始位置更新,则“核心位置”框架会提示用户确认是否应重新启用位置服务

So if your app has parts that runs without locations, then dont start location updates at app start. 因此,如果您的应用程序的某些部分没有位置运行,则不要在应用程序启动时启动位置更新。 once the user chooses a feature that needs location updates read the value of [CLLocationManager locationServicesEnabled] . 用户选择需要位置更新的功能后,请读取[CLLocationManager locationServicesEnabled]的值。
if false post an alert, like Apple maps when you press the arrow (Locate me) button. 如果为false,则发布警报,例如当您按下箭头(找到我)按钮时,苹果地图。
if true start location updates. 如果为true,则开始位置更新。

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

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