iOS locationmanager didChangeAuthorizationStatus未从kCLAuthorizationStatusDenied调用到kCLAuthorizationStatusAuthorized状态

时间:2015-12-07 08:53:54

标签: ios core-location cllocationmanager

在设置app>中,didChangeAuthorizationStatus被称为kCLAuthorizationStatusDenied状态。隐私>位置服务>我的应用>从不在后台模式。 但在更改' Always'之后,不会调用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);
}

对不起我的英语。

2 个答案:

答案 0 :(得分:5)

我遇到了这个确切的问题,原来我正在创建我的CLLocationManager并致电

-requestWhenInUseAuthorization

在后台线程上。我在主线程上调用了这些方法并得到了预期的结果(我点击了#34; Don不允许"或者#34;允许"} #34)

答案 1 :(得分:1)

总结Newtz评论......

Swift 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()
}