James
James

Reputation: 6509

If location services is turned off, CLLocationManager never updates

If a user has turned off location services (either for all apps or just mine), CLLocationManager never prompts and never updates the position. This is to be expected, but is there a way to detect that location services is turned off, and prompt to reenable it?

Upvotes: 2

Views: 4621

Answers (4)

HalR
HalR

Reputation: 11083

I think a better way to approach this would be to do this:

switch ([CLLocationManager authorizationStatus]) {
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    case kCLAuthorizationStatusAuthorizedAlways:
        //we're good to go
        break;
    default:
        //pop up an alert
        break;
}

Upvotes: 0

matm
matm

Reputation: 7079

Just to complete Tim's answer.

You can always try starting location updates via startUpdatingLocation method on CLLocationManager instance. If location services are disabled, you'll get notified about error condition by delegate and then you can bring up the dialog asking user to go into Settings and enable location services for your app...

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)inManager didFailWithError:(NSError *)inError{
    if (inError.code ==  kCLErrorDenied) {
        NSLog(@"Location manager denied access - kCLErrorDenied");
        // your code to show UIAlertView telling user to re-enable location services
        // for your app so they can benefit from extra functionality offered by app
    }
}

Please note that you can launch Settings app via URL-scheme on iOS5 (versions lower than 5.0 (and greater than 5.0), don't support it). Call:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs://"]];

Upvotes: 5

superjessi
superjessi

Reputation: 1790

You can check if location services are enabled, before you try and update the location.

Here is a snippet of the code I use:

if (TARGET_IPHONE_SIMULATOR) {
    currentLocation = [[CLLocation alloc] initWithLatitude:37.331718 longitude:-122.030629];
    [self methodNameHere:currentLocation];
} else {
    if ([CLLocationManager locationServicesEnabled]) {
        [locationManager startUpdatingLocation];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"Enable location services to do this" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

Basically, the code checks if you're targeting the simulator, which can give you errors if you try to get the location, so instead I just hard-code the location I want to simulate (in this case, Apple HQ). If targeting the device, it checks if location services are enabled, if so, it calls the method you want to perform, if not, it shows the user an alert message.

Upvotes: 1

Tim Hoolihan
Tim Hoolihan

Reputation: 12396

According to the docs, CLLocationManager has a static method to see if LocationServices are enabled:

+ (BOOL)locationServicesEnabled

Upvotes: 9

Related Questions