问题
I tested my device (iPod Touch 2G iOS 4.1) if location 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.
回答1:
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.
回答2:
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]
回答3:
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.
回答4:
Swift 3.1 function returns -> status:Bool and message:String
func isLocationEnabled() -> (status: Bool, message: String) {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .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")
}
}
回答5:
if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways))
{
; // app doesn't have access to localization to whatever you want
}
回答6:
[CLLocationManager locationServicesEnabled] will return NO when the user setting button is switched to OFF, only then I have achieved a NO.
来源:https://stackoverflow.com/questions/4034095/locationservicesenabled-always-return-yes