why CLLocationManager always returns true?

2019-07-30 15:04发布

问题:

I am using [CLLocationManager locationServicesEnabled] function to get the status whether location service is enabled or not. I kept code in viewDidLoad method.

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

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?

回答1:

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.