如何在位置权限 [iOS] Flutter 中显示“始终允许”选项?

Sar*_*ekR 5 dart flutter flutter-dependencies

我的应用程序在后台以及当应用程序处于终止状态时获取用户的当前位置。

因此它需要“始终允许”权限级别,但在 iOS 中我只得到两个选项,“允许一次”“使用时”

我正在使用permission_handler包来请求权限;

 PermissionStatus permission = await Permission.locationAlways.request();
Run Code Online (Sandbox Code Playgroud)

但它仍然没有显示“始终允许”选项。 在此输入图像描述

此问题仅在 iOS 中出现。在 Android 中,一切正常。

Mar*_*uez 10

正如permission_handler的文档所说:

不能直接请求locationAlways权限,用户必须先请求locationWhenInUse权限。通过单击“使用应用程序时允许”接受此权限使用户可以请求 locationAlways 权限。然后,这将弹​​出另一个权限弹出窗口,要求您仅在使用时保留或更改为始终允许。

这就是为什么没有出现在第一个对话框中的原因。

现在,为了显示第二个对话框,您可以在其中请求用户始终允许,您必须在使用时请求权限,然后验证状态,请求第二个权限

你可以这样做:

    var status = await Permission.locationWhenInUse.status;
if(!status.isGranted){
  var status = await Permission.locationWhenInUse.request();
  if(status.isGranted){
    var status = await Permission.locationAlways.request();
    if(status.isGranted){
      //Do some stuff
    }else{
      //Do another stuff
    }
  }else{
    //The user deny the permission
  }
  if(status.isPermanentlyDenied){
    //When the user previously rejected the permission and select never ask again
    //Open the screen of settings
    bool res = await openAppSettings();
  }
}else{
  //In use is available, check the always in use
  var status = await Permission.locationAlways.status;
  if(!status.isGranted){
    var status = await Permission.locationAlways.request();
    if(status.isGranted){
      //Do some stuff
    }else{
      //Do another stuff
    }
  }else{
    //previously available, do some stuff or nothing
  }
}
Run Code Online (Sandbox Code Playgroud)

快乐编码!