Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didChangeAuthorizationStatus not called after changing status

I have a problem where the locationManager:didChangeAuthorizationStatus is not being called after the user either (1) accepts or (2) declines my request to use location services. By placing out different NSLog statements, I have reached the conclusion that the method is called when I request authorization, but not when the user makes a choice. Have anyone had the same issues? If so, how did you solve them?

Here's how I initialize my location manager:

if (_locationManager == nil) {
  NSLog(@"Creating location manager");
  _locationManager = [[CLLocationManager alloc] init];
  _locationManager.delegate = self;
  _locationManager.distanceFilter = kCLDistanceFilterNone;
  _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
  NSLog(@"Not determined");
  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"dynamicNotifOn"]) {
    [_locationManager requestAlwaysAuthorization];
  } else {
    [_locationManager requestWhenInUseAuthorization];
  }
} else if (...) {...

and here's the method:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  NSLog(@"Callback");
  if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
    NSLog(@"Authorized");
    [_mainButton setIsLoading:NO];
    [self startGettingLocation];
  } else if (status == kCLAuthorizationStatusDenied || status == kCLAuthorizationStatusRestricted) {
    NSLog(@"Denied");
    _currentState = CurrentStateError;
    [_mainButton setUpButtonForState:_currentState];
  }
}

After I press the button that initializes the location manager (the top code-block), this is what the console prints out:

Creating location manager
Not determined
Callback

And then I make a choice in the AlertView that pops up:

*nothing*
like image 693
Aleksander Avatar asked Feb 11 '23 04:02

Aleksander


1 Answers

In my case the problem was in non-main thread. Just make sure you creating location manager on the main thread.

like image 119
AlexeyVMP Avatar answered Apr 30 '23 20:04

AlexeyVMP