ios - 更改状态后未调用 didChangeAuthorization Status

标签 ios objective-c cllocationmanager

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

下面是我如何初始化我的位置管理器:

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*

最佳答案

在我的例子中,问题出在非主线程中。 只需确保在主线程上创建位置管理器即可。

关于ios - 更改状态后未调用 didChangeAuthorization Status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27955048/

相关文章:

ios - 在 UItableView 滚动时显示/隐藏顶部 View 和底部 View

iphone - 右对齐和方向改变时 UITextField 占位符文本位置错误

ios - iPad 上的 iPhone 应用程序 : drawViewHierarchyInRect causes glitch

iOS 在设置中检查 3D Touch 启用

iphone - 使用 App store 对可执行文件进行了无效授权签名

swift - 获得准确的罗盘航向(磁航向和精度)

ios - CLLocationManager() Location 在模拟器中为 nil

iphone - 标签栏似乎被锁定(默认设置)

ios - Apple 拒绝因为 UIBackgroundMode = location

iOS - 在不使用 CLLocationManager 的情况下确定用户的国家