我遇到一个问题,在用户 (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/