首页 > 解决方案 > 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

标签: ioscore-locationios13ios-permissions

解决方案


这有点令人困惑,不是吗?当您要求 Always 并且用户点击 Allow Once 时,您会被告知您获得了 WhenInUse。但这实际上并不重要。你有临时的总是。所以:

  • 当您随后进入后台并开始监控访问或区域或任何您的位置监控使用情况时,这将转换为始终授权以用于使用目的。(您的日志记录应确认这一点。)

  • 然后,因为你只获得了一次授权,当你回到前台时,你将再次未确定。

所以要点是,笑一个邪恶的笑,然后继续前进。您的后台位置监控将起作用,这才是最重要的。它不仅有效,而且作为奖励,您可以再次向用户显示授权警报,这就是 iOS 13 中所有这些更改的原因。别担心,开心点。


推荐阅读