I tested my device (iPod Touch 2G iOS 4.1) if location services are enabled
permitted = [locationManager locationServicesEnabled];
and I always
When you use
[CLLocationManager locationServicesEnabled]
then you inspect if locationServices are enabled in whole system. So when you go to Settings -> Location Services and you see that first switch. That method returns state of that state and is not in relation with your app.
If you need to know if your app has access to location services use @Pascalius answer.
Swift 3.1 function returns -> status:Bool and message:String
func isLocationEnabled() -> (status: Bool, message: String) {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .restricted, .denied:
return (false,"No access")
case .authorizedAlways, .authorizedWhenInUse:
return(true,"Access")
}
} else {
return(false,"Turn On Location Services to Allow App to Determine Your Location")
}
}
Remember that [locationManager locationServicesEnabled]
is deprecated since iOS 4.0.
Use the Class Method [CLLocationManager locationServicesEnabled]
instead.
The App Specific Button can be retrieved by
[CLLocationManager authorizationStatus]
if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways))
{
; // app doesn't have access to localization to whatever you want
}
[CLLocationManager locationServicesEnabled] will return NO when the user setting button is switched to OFF, only then I have achieved a NO.
When you implement the delegate for location manager, you should be implementing didFailWithError. In there you will get the appropriate error if the user did not allow access to location
Apple Documentation States:
If the user denies your application’s use of the location service, this method reports a kCLErrorDenied
error. Upon receiving such an error, you should stop the location service.