【问题标题】:how to show "Always allow" option in location permission [iOS] Flutter?如何在位置权限 [iOS] Flutter 中显示“始终允许”选项?
【发布时间】:2021-07-03 05:24:57
【问题描述】:
我的应用在后台获取用户的当前位置以及应用何时处于终止状态。
所以它需要 “始终允许” 权限级别,但在 iOS 中我只有两个选项,“允许一次” 和 “在使用”。
我正在使用permission_handler 包请求权限;
PermissionStatus permission = await Permission.locationAlways.request();
但仍然不显示“始终允许”选项。
此问题仅在 iOS 中。在 Android 中,一切正常。
【问题讨论】:
标签:
flutter
dart
flutter-dependencies
【解决方案1】:
正如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
}
}
编码愉快!