Stefan Stefan - 5 months ago 16
iOS Question

CLLocationManager permissions

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];
}

Answer

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.)