繁体   English   中英

Flutter permission_handler:请求iOS的位置权限

[英]Flutter permission_handler : request location permission on iOS

我想要求用户仅在拒绝权限或不允许权限时才启用权限此 function 在 android 上运行良好

  Future _getLocationPermission() async {
    if (await Permission.location.request().isGranted) {
      permissionGranted = true;
    } else if (await Permission.location.request().isPermanentlyDenied) {
      throw('location.request().isPermanentlyDenied');
    } else if (await Permission.location.request().isDenied) {
      throw('location.request().isDenied');
      permissionGranted = false;
    }
  }

但在 iOS 上,它抛出异常权限 isPermanentlyDenied Unhandled Exception: location.request().isPermanentlyDenied 即使用户在使用我正在使用的应用程序时允许位置权限,我正在使用permission_handler package

iOS 更严格,如果用户已经永久拒绝,则不允许您请求权限。 在这种情况下,您唯一的选择是通知用户这一点,并提供打开应用程序设置并在那里授予权限的可能性。 如果用户同意,这可能会重新启动您的应用程序。

因此,无需请求即可检查状态:

final status = await Permission.location.status;

如果状态为永久拒绝,则显示 Flutter 对话框(在这种情况下您不能使用系统权限授予对话框):

if (status == PermissionStatus.permanentlyDenied) {
  // display a dialog, explain the user that he/she can grant 
  // permission only in the phone's application settings
}

如果用户想要这样做,您可以路由到应用程序设置:

openAppSettings(); // this is a method of the permission handler package

这种方法是一种未来,但根据我的经验,你不需要等待它。

请检查此链接 您还必须在 POD 文件中提及权限。

这只是一个 function 如果用户启用位置或不简单则返回 bool

Future<bool> canGetLocation(Location location) async {
      if (!await location.requestService()) return false;
       final status = await location.requestPermission();
       final granted = PermissionStatus.granted;
       final grantedLimited = PermissionStatus.grantedLimited;
       bool result = status == granted || status == granted;
      return result;
    } 
    
    if (await canGetLocation(location)) {
         // Do something
        } else {
          final status = await handler.Permission.location.status;
          if (status.isPermanentlyDenied || status.isDenied) {
           // ask the user to open the app setting 
           // from permission handler package you have 
               openAppSettings(); 
          } else {
            // show info dialog or something that the user need to enable location services
          }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM