为什么CLLocationManager总是返回true? [英] why CLLocationManager always returns true?

查看:126
本文介绍了为什么CLLocationManager总是返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用[CLLocationManager locationServicesEnabled]函数获取状态(无论是否启用了定位服务).我将代码保存在viewDidLoad方法中.

if(![CLLocationManager locationServicesEnabled])
{
    NSLog(@"No");
}
else
{
    NSLog(@"Yes");
}

当我第一次运行该应用程序时,它会返回Yes.为什么?它应该返回我No.当我没有选择允许"或不允许"选项时,就是这种情况.表示我既不允许也不不允许,但是在viewDidLoad中我得到了Yes.

现在,我选择不允许",然后再次加载相同的viewController.至少在这个时候它应该返回我No,但是我仍然得到了Yes.为什么?

CLLocationManager如此混乱.

有什么想法吗?

解决方案

locationServicesEnabled返回是否在设置中启用了位置服务.如果在设置中启用了位置服务,则此功能将始终返回YES.

来自文档

locationServicesEnabled

返回一个布尔值,指示是否在设备上启用了位置服务.

讨论

用户可以通过设置"启用或禁用位置服务 一般通过切换位置服务"开关来实现该应用程序.

在开始之前,您应该检查此方法的返回值 位置更新,以确定用户是否具有位置服务 为当前设备启用.如果此方法返回否,则您 无论如何开始位置更新,Core Location框架都会提示 用户确认是否应重新启用位置服务.

(是否在警报视图中)用户允许/拒绝的应用程序权限都不会影响此方法的返回值.

如果您想知道用户是否已授予应用程序访问位置的权限,则可以使用

When I am running this app first time it returns Yes. Why? It should return me No. This is the case when I did not select "Allow" or "Don't allow" options. Means I neither allow nor don't allow but in viewDidLoad I got Yes.

Now I select "Don't allow" and again load the same viewController. At this time at least it should return me No, but still I got Yes. Why?

So much of confusion with CLLocationManager.

Any ideas?

解决方案

locationServicesEnabled returns whether location service is enabled on settings.. If it is enabled in settings, this function returns YES all the time..

from documentation

locationServicesEnabled

Returns a Boolean value indicating whether location services are enabled on the device.

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.

Whether or not user allowed/rejected app permission (in the alertview) doesn't affect the return value of this method.

If you want to know whether user has given application permission to access location, you can use authorizationStatus.

这篇关于为什么CLLocationManager总是返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆