iOS locationmanager didChangeAuthorizationStatus 未从 kCLAuthorizationStatusDenied 调用到 kCLAuthorizatio
Posted
技术标签:
【中文标题】iOS locationmanager didChangeAuthorizationStatus 未从 kCLAuthorizationStatusDenied 调用到 kCLAuthorizationStatusAuthorized 状态【英文标题】:iOS locationmanager didChangeAuthorizationStatus not called from kCLAuthorizationStatusDenied to kCLAuthorizationStatusAuthorized status 【发布时间】: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);
对不起我的英语。
【问题讨论】:
【参考方案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()
【讨论】:
以上是关于iOS locationmanager didChangeAuthorizationStatus 未从 kCLAuthorizationStatusDenied 调用到 kCLAuthorizatio的主要内容,如果未能解决你的问题,请参考以下文章
iOS locationmanager didChangeAuthorizationStatus 未从 kCLAuthorizationStatusDenied 调用到 kCLAuthorizatio
iOS LocationManager 未更新位置(Titanium Appcelerator 模块)
iOS 8 中的 didEnterRegion - 区域监控