更改状态后未调用didChangeAuthorizationStatus

时间:2015-01-15 00:38:52

标签: ios objective-c cllocationmanager

我遇到一个问题,即在用户(1)接受或(2)拒绝我使用位置服务的请求后,locationManager:didChangeAuthorizationStatus未被调用。通过放置不同的NSLog语句,我得出的结论是,当我请求授权时调用该方法,而不是在用户做出选择时调用。有没有人有同样的问题?如果是这样,你是如何解决的?

以下是我初始化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 (...) {...

这是方法:

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

按下初始化位置管理器(顶部代码块)的按钮后,这就是控制台打印出的内容:

Creating location manager
Not determined
Callback

然后我在弹出的AlertView中做出选择:

*nothing*

3 个答案:

答案 0 :(得分:6)

在我的情况下,问题出在非主线程中。 只需确保在主线程上创建位置管理器。

答案 1 :(得分:2)

上面的例子中有一些奇怪的事情:

  1. 委托回调locationManager:didChangeAuthorizationStatus:是否以NotDetermined状态调用?!?在文档中明确写出,只有在授权状态发生变化时才会调用委托回调方法,但在上面的示例中没有。可能是一个错误。

  2. 在Paulw11中,答案表明即使用户已获得授权申请,我们也必须始终请求许可?!?根据我的观点,应用程序首先必须调用CLLocationManager.authorizationStatus并仅在授权为NotDetermined(即用户仍未授权我们的应用程序)时请求授权 - 这也在Apple文档中说明。

答案 2 :(得分:0)

您不应该检查kCLAuthorizationStatusNotDetermined - 只需每次都要求授权 -

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

  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"dynamicNotifOn"]) {
     [self.locationManager requestAlwaysAuthorization];
  } else {
     [self.locationManager requestWhenInUseAuthorization];
  }
}

当我使用你的委托方法时,我得到以下输出 -

2015-01-15 12:07:43.357 LocationAuthDemo[9942:500036] Callback
2015-01-15 12:07:51.389 LocationAuthDemo[9942:500036] Callback
2015-01-15 12:07:51.389 LocationAuthDemo[9942:500036] Authorized

您从委托方法更新UI元素调用的任何方法是什么?如果是这样,你应该在主队列上发送它们。

相关问题