Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

locationServicesEnabled test passes when they are disabled in viewDidLoad

I have location services disabled for my application in the settings panel. I run a test in viewDidLoad in my view controller to see if they are enabled:

if([CLLocationManager locationServicesEnabled]) {
   //Do something now
}

This test always passes for some reason. If I try and access location services I get a kCLErrorDenied error for the location manager. What gives?

Am I using the wrong test?

like image 214
Adam Avatar asked Aug 04 '10 00:08

Adam


2 Answers

The locationServicesEnabled class method only tests the global setting for Location Services. AFAIK, there's no way to test if your app has explicitly been denied. You'll have to wait for the location request to fail and use the CLLocationManagerDelegate method locationManager:didFailWithError: to do whatever you need. E.g.:

- (void)locationManager: (CLLocationManager *)manager
       didFailWithError: (NSError *)error {

    NSString *errorString;
    [manager stopUpdatingLocation];
    NSLog(@"Error: %@",[error localizedDescription]);
    switch([error code]) {
        case kCLErrorDenied:
            //Access denied by user
            errorString = @"Access to Location Services denied by user";
            //Do something...
            break;
        case kCLErrorLocationUnknown:
            //Probably temporary...
            errorString = @"Location data unavailable";
            //Do something else...
            break;
        default:
            errorString = @"An unknown error has occurred";
            break;
        }
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

See the documentation on the CLError constants in the CLLocationManager class reference for more options.

like image 189
Dan Avatar answered Nov 16 '22 00:11

Dan


iOS 4.2 now allows one to determine if location services have been denied through the CLLocationManager +authorizationStatus method.

like image 28
GBegen Avatar answered Nov 16 '22 00:11

GBegen