locationServicesEnabled always return YES
I tested my device (iPod Touch 2G iOS 4.1) if locati开发者_Python百科on services are enabled
permitted = [locationManager locationServicesEnabled];
and I always get a YES whether location services are enabled or not. I'm talking about the general button for location services and not the app specific button. On iPad with iOS 3.2.2 everything is working fine.
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]
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.
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.
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")
}
}
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.
精彩评论