location = [[CLLocationManager alloc] init];
location.desiredAccuracy = kCLLocationAccurac开发者_高级运维yBestForNavigation ;
location.distanceFilter = 10 ;
location.delegate=self;
locationEnabledBool = [CLLocationManager locationServicesEnabled];
if (locationEnabledBool ==NO) {
UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
}
else
[location startUpdatingLocation];
The value of locationEnabledBool
is always YES
, independent of whether or not location services are enabled. Can any body help?
instead of
if (locationEnabledBool == NO) {
//give error message
}
try
if ( [CLLocationManager locationServicesEnabled] ==NO || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
//give error message
}
I found this on the link.
Detecting whether location services are enabled for my app
When you are testing this code, you will need to make sure that you test it on a device, and not just using the iOS Simulator.
Also, I suggest that you double check in the Settings on that device to make sure that Location Services, on the first page of the settings, says Off.
精彩评论