当用户在 iOS 中不允许 AuthorizedAlways 时,是否可以回退到 AuthorizedWhenInUse?

Is it possible to fallback to AuthorizedWhenInUse when a user doesn't allow AuthorizedAlways in iOS?

我的应用在后台使用用户位置,但有时用户不允许应用始终收集 GPS 数据。该应用程序只能处理前景位置,我想将其设置为后备。

有没有一种优雅的方式,一旦 iOS 用户拒绝了我的 AuthorizedAlways 请求,重新提示用户授予 AuthorizedWhenInUse 权限?

你不能强迫,但你可以:

1) 知道用户被拒绝许可 2) 并显示一个警告询问:"Please, go to settings and enable it".
3) 转到 ios 设置 ([[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];)

像这样:

- (void)checkLocation
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    if (status == kCLAuthorizationStatusAuthorizedAlways){
        NSLog(@"ok");
    } else if(status == kCLAuthorizationStatusDenied ||
              status == kCLAuthorizationStatusAuthorizedWhenInUse){
        [self showRequestLocationMessage];
    } else if(status == kCLAuthorizationStatusNotDetermined){
        //request auth
    }

}

- (void)showRequestLocationMessage
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Settings"
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * alertAction){
                                                       [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];
                                                   }];

    NSString *title = @"Service Enable";
    NSString *text = @"Please enable your location.. bla bla bla";

    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:title
                                          message:text
                                          preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action){
                                                             [alertController dismissViewControllerAnimated:YES completion:nil];
                                                         }];

    [alertController addAction:cancelAction];
    [alertController addAction:action];

    [self presentViewController:alertController animated:YES completion:nil];
}