0

I'm working on permissions for location, problem is next: User turned off location services from privacy and installed the app. I have line of code that is asking to enable location services: if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)... and this is working pretty fine. Problem is that app don't ask for allowing app to use location, then it asks the second time. Code for asking for permission:

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

NOTE: Everything works fine if locations services enabled, it asks for approval first time.

EDIT: Full code for permissions:

-(void)setupPermissions
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

    }

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    [self.locationManager startUpdatingLocation];
}
Stefan
  • 1,283
  • 15
  • 33

2 Answers2

2

Your code makes no sense. You have this:

if (something) {
}
if (somethingelse) {
    [self.locationManager requestWhenInUseAuthorization];
}

So you have one if that is empty, and another if that always runs regardless of what the first if does. Perhaps you meant to use else if for the second condition?

(And keep in mind that, as you've already been told, calling requestWhenInUseAuthorization is pointless unless the status is NotDetermined.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes, I removed empty if. But how can I solve this problem to "track" is user changed location services to on and to ask "live" to allow? – Stefan Jun 24 '16 at 21:00
  • If the user _changes_ location services, the location manager delegate will get `locationManager:didChangeAuthorizationStatus:`. – matt Jun 24 '16 at 21:06
1

The only time you can get iOS to request user permissions is when the authorization status is NotDetermined.

If a user has denied your app, the only way you can get them to be prompted again is to uninstall the app, or reset privacy on the device.

The easiest approach would be to provide an alert controller with a link to settings so that they can turn it on themselves.

Here is a link that shows the process for doing that.

Community
  • 1
  • 1
CodeBender
  • 35,668
  • 12
  • 125
  • 132