ios - iOS 13 仅在用户“允许一次”后再次调用 requestAlwaysAuthorization
问题描述
在 iOS13 上请求 Always 权限期间,用户可以点击“允许一次”,这将调用具有状态的适当委托,kCLAuthorizationStatusAuthorizedWhenInUse
但再次请求“始终”以 调用委托kCLAuthorizationStatusAuthorizedAlways
。为什么?当其他组合像您总是要求的那样只工作一次时,您会得到它,即使再次调用也不会调用具有状态的委托。
要测试的示例代码:
@import CoreLocation;
@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
- (IBAction)doauthloc:(id)sender {
[self.locationManager requestAlwaysAuthorization];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch(status) {
case kCLAuthorizationStatusNotDetermined:NSLog(@"AUTH STATUS:kCLAuthorizationStatusNotDetermined"); break;
case kCLAuthorizationStatusRestricted:NSLog(@"AUTH STATUS:kCLAuthorizationStatusRestricted"); break;
case kCLAuthorizationStatusDenied:NSLog(@"AUTH STATUS:kCLAuthorizationStatusDenied"); break;
case kCLAuthorizationStatusAuthorizedAlways:NSLog(@"AUTH STATUS:kCLAuthorizationStatusAuthorizedAlways"); break;
case kCLAuthorizationStatusAuthorizedWhenInUse:NSLog(@"AUTH STATUS:kCLAuthorizationStatusAuthorizedWhenInUse"); break;
};
}
@end
解决方案
这有点令人困惑,不是吗?当您要求 Always 并且用户点击 Allow Once 时,您会被告知您获得了 WhenInUse。但这实际上并不重要。你有临时的总是。所以:
当您随后进入后台并开始监控访问或区域或任何您的位置监控使用情况时,这将转换为始终授权以用于使用目的。(您的日志记录应确认这一点。)
然后,因为你只获得了一次授权,当你回到前台时,你将再次未确定。
所以要点是,笑一个邪恶的笑,然后继续前进。您的后台位置监控将起作用,这才是最重要的。它不仅有效,而且作为奖励,您可以再次向用户显示授权警报,这就是 iOS 13 中所有这些更改的原因。别担心,开心点。
推荐阅读
- css - React - 如何在同一行对齐文本和img
- google-sheets - 如何删除谷歌表格中的空白行
- javascript - 有没有办法打开/关闭侧边栏
- python - 在 scipy 中连接稀疏矩阵中的特定行
- angular - Angular 上随处可用的数据
- reactjs - 嘿,我该如何解决 SyntaxError: D:\complaint_reactjs\node_modules\watchpack\lib\chokidar.js: 'return' outside of function (4:1) Reactjs
- javascript - 向列表添加名称,在模式中删除其输入字段
- python - 我正在尝试在 python ctypes 中将 float 转换为 str 但它永远不会发生
- python-3.x - tkinter我如何计算法向量和python中所有粒子的动能守恒?
- html - 为什么我的页面在调整窗口大小时与在 Chrome 移动查看器中看起来不同?