【问题标题】:iOS locationmanager didChangeAuthorizationStatus not called from kCLAuthorizationStatusDenied to kCLAuthorizationStatusAuthorized statusiOS locationmanager didChangeAuthorizationStatus 未从 kCLAuthorizationStatusDenied 调用到 kCLAuthorizationStatusAuthorized 状态
【发布时间】:2015-12-07 08:53:54
【问题描述】:

在设置应用 > 隐私 > 定位服务 > 我的应用 > 从不处于后台模式时,didChangeAuthorizationStatus 被称为 kCLAuthorizationStatusDenied 状态。 但是在更改“始终”之后,不会调用 didChangeAuthorizationStatus。 关闭“定位服务”时也不会调用。

我在 Info.plist 中插入了 NSLocationAlwaysUsageDescription 并设置了位置更新后台模式。

我的代码,

- (id)init {
    self = [super init];
    if (self) {       
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.pausesLocationUpdatesAutomatically = NO;

        if ([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
            NSLog(@"setAllowsBackgroundLocationUpdates");
            _locationManager.allowsBackgroundLocationUpdates = YES;
        }

        [self requestAuthorization];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"CLAuthorizationStatus: %d", status);
}

对不起我的英语。

【问题讨论】:

    标签: ios core-location cllocationmanager


    【解决方案1】:

    我遇到了这个确切的问题,结果我正在创建我的 CLLocationManager 并调用

    -requestWhenInUseAuthorization

    在后台线程上。我在主线程上调用了这些方法并得到了预期的结果(-locationManager:didChangedAithorizationStatus: 在我点击“不允许”或“允许”后被调用)

    【讨论】:

    • 终于有办法了!正如@LOP_Luke 所暗示的那样,我发现我必须在主线程上同时进行 CLLocationManager 初始化和权限请求,而不仅仅是权限请求
    【解决方案2】:

    总结 Newtz 的评论......

    斯威夫特 3

    var locationManager:CLLocationManager? = .none
    

    ...

    // Set up your location manager as desired
    DispatchQueue.main.async {
          self.locationManager = CLLocationManager()
          self.locationManager?.delegate = self
          self.locationManager?.activityType = self.activityType
          self.locationManager?.desiredAccuracy = self.desiredAccuracy
          self.locationManager?.distanceFilter = self.distanceFilter
          self.locationManager?.allowsBackgroundLocationUpdates = true
          self.locationManager?.pausesLocationUpdatesAutomatically = false
      }
    

    ...

    // Authorize
    DispatchQueue.main.async {
        // If you're not in the background you like need requestWhenInUseAuthorization()
        self.locationManager?.requestAlwaysAuthorization()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      相关资源
      最近更新 更多