Andrey Chernukha
Andrey Chernukha

Reputation: 21808

CLLocationManager doesn't show popup if location services were previously disabled

When I launch my app I check current location authorisation status like this:

- (void)checkCurrentStatus
{
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)
{
   [self.locationManager requestWhenInUseAuthorization];
}
else
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied && ![CLLocationManager locationServicesEnabled])
{
    [self.locationManager startUpdatingLocation];
}
}

If overall location services are enabled (for the whole device) than it just asks user for permission, the alert pops up. If they are disabled (else if condition) then I need to call startUpdatingLocation because call to [self.locationManager requestWhenInUseAuthorization]; has no effect when authorisation status is kCLAuthorizationStatusDenied (without any if conditions). Ok, so I call startUpdatingLocation and then alert pops up which says:

Turn On Location Services to Allow "AppName" to Determine Your Location

Ok, I go to the settings on turn overall location services on. After that authorizationStatus becomes kCLAuthorizationStatusNotDetermined but when I call requestWhenInUseAuthorization it has no effect! No popup appears, user is not prompted for to authorise location, the status remains the same, I can't use location manager. How can I handle that?

Upvotes: 4

Views: 4421

Answers (1)

Vinzzz
Vinzzz

Reputation: 11724

From Apple's doc on CLLocationManager about - (void)requestWhenInUseAuthorization

If the current authorization status is anything other than kCLAuthorizationStatusNotDetermined, this method does nothing and does not call the locationManager:didChangeAuthorizationStatus: method

Here's what you need :

- (void)requestAlwaysAuthorization
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    // If the status is denied or only granted for when in use, display an alert
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
        NSString *title;
        title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
        NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"Settings", nil];
        [alertView show];
    }
    // The user has not enabled any location services. Request background authorization.
    else if (status == kCLAuthorizationStatusNotDetermined) {
        [self.locationManager requestAlwaysAuthorization];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // Send the user to the Settings for this app
        NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        [[UIApplication sharedApplication] openURL:settingsURL];
    }
}

Courtesy copy of this blog post

Upvotes: 5

Related Questions