如何在Flutter中请求和检查权限 [英] How to request and check permissions in Flutter

查看:235
本文介绍了如何在Flutter中请求和检查权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击不允许"时,我正在使用各种插件来获取用户数据,联系人,照片和相机,该应用程序将保持静默状态.我想在用户选中``不再询问并且不允许''并再次进入应用程序时向用户显示``询问权限''对话框.

当前,当用户选中不允许"时,应用程序再也不会询问

我知道用户必须授予应用访问权限才能访问个人信息,包括当前位置相机日历联系人 mediaLibrary 麦克风传感器语音和照片.尽管人们喜欢使用可以访问此信息的应用程序所带来的便利,但他们也希望能够控制自己的私人数据.例如,人们喜欢能够自动标记其实际位置的照片或找到附近的朋友,但他们也希望选择禁用这些功能.

如何再次扑通用户权限?

解决方案

我对此问题感到非常烦恼.应用了几种解决方案后,我发现了该解决方案.所以我想与大家分享,这就是为什么我问这个问题,然后我回答了

在大多数操作系统上,安装时并不仅授予应用程序权限.相反,开发人员必须在应用程序运行时向用户询问权限.

处理权限的最佳方法是使用

I am using a various plugin to get user data, contact, photos and camera when the user clicks Don't allow, The application goes silent. I want to show the user the ask permission dialog when the user checks the Never ask again and Don't allow and enters the application again.

Currently the app never ask to again when the user checks Don't allow

I Know Users must grant permission for an app to access personal information, including the current location, camera, calendar, contacts, mediaLibrary, microphone, sensors, speech and photos. Although people appreciate the convenience of using an app that has access to this information, they also expect to have control over their private data. For example, people like being able to automatically tag photos with their physical location or find nearby friends, but they also want the option to disable such features.

How to ask for user permission again in flutter?

解决方案

I was very troubled with this issue After applying several solutions I found this solution. So I want to share it with everyone, that's why I asked the question and I answered

On most operating systems, permissions aren't just granted to apps at install time. Rather, developers have to ask the user for permissions while the app is running.

The best way to handle permissions is by using the permission_handler plugin. This plugin provides a cross-platform (iOS, Android) API to request permissions and check their status.

We can also open the device's app settings so users can grant permission. On Android, we can show a rationale for requesting permission.

  1. Add this to your package's pubspec.yaml file:

    dependencies:
      permission_handler: ^5.0.1+1
    

  2. Now in your Dart code, you can use:

    import 'package:permission_handler/permission_handler.dart';
    

  3. While the permissions are being requested during runtime, you'll still need to tell the OS which permissions your app might potentially use. That requires adding permission configuration to Android- and iOS-specific files.

    iOS

    • Add permission to your Info.plist file. Here's an example Info.plist with a complete list of all possible permissions.

    Android

    1. Add the following to your "gradle.properties" file:

      android.useAndroidX=true
      android.enableJetifier=true
      

    2. Make sure you set the compileSdkVersion in your "android/app/build.gradle" file to 28:

      android {
        compileSdkVersion 28
        ...
      }
      

    3. Make sure you replace all the android. dependencies to their AndroidX counterparts (a full list can be found here: https://developer.android.com/jetpack/androidx/migrate)

    Add permissions to your AndroidManifest.xml file. There's a debugmain and profile version which are chosen depending on how you start your app. In general, it's sufficient to add permission only to the main version. Here's an example AndroidManifest.xml with a complete list of all possible permissions.

  4. There are a number of Permissions. You can get a Permission's status, which is either undeterminedgranteddeniedrestricted or permanentlyDenied.

    var status = await Permission.camera.status;
    if (status.isUndetermined) {
      // We didn't ask for permission yet.
    }
    
    // You can can also directly ask the permission about its status.
    if (await Permission.location.isRestricted) {
       // The OS restricts access, for example because of parental controls.
    }
    

    Call request() on a Permission to request it. If it has already been granted before, nothing happens. request() returns the new status of the Permission.

    if (await Permission.contacts.request().isGranted) {
      // Either the permission was already granted before or the user just granted it.
    }
    
    // You can request multiple permissions at once.
    Map<Permission, PermissionStatus> statuses = await [
      Permission.location,
      Permission.storage,
    ].request();
    print(statuses[Permission.location]);
    

    On Android, you can show a rationale for using a permission:

    bool isShown = await Permission.contacts.shouldShowRequestRationale;
    

Full Example

Container(
  child: Wrap(
    children: <Widget>[
      ListTile(
          leading: Icon(Icons.camera_enhance),
          title: Text(getTranslated(context, "Camera")),
          onTap: () async {
            var status = await Permission.camera.status;
            if (status.isGranted) {
              final pickedFile =
                  await _picker.getImage(source: ImageSource.camera);
              final File file = File(pickedFile.path);
              imageSelected(file);
            } else if (status.isUndetermined) {
              final pickedFile =
                  await _picker.getImage(source: ImageSource.camera);
              final File file = File(pickedFile.path);
              imageSelected(file);
            } else {
              showDialog(
                  context: context,
                  builder: (BuildContext context) => CupertinoAlertDialog(
                        title: Text('Camera Permission'),
                        content: Text(
                            'This app needs camera access to take pictures for upload user profile photo'),
                        actions: <Widget>[
                          CupertinoDialogAction(
                            child: Text('Deny'),
                            onPressed: () => Navigator.of(context).pop(),
                          ),
                          CupertinoDialogAction(
                            child: Text('Settings'),
                            onPressed: () => openAppSettings(),
                          ),
                        ],
                      ));
            }
          }),
    ],
  ),
)

这篇关于如何在Flutter中请求和检查权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆