This is a library of dynamic permission processing plugins for Flutter that allows developers of the Flutter application layer to handle native dynamic permissions in a very simple API. It encapsulates checks on permissions, requests, and when permissions are permanently denied, the appropriate system Settings page is pulled up to prompt the user to manually open permissions. It’s hard to think of a reason not to use it.

Warehouse address: flutter_easy_permission

  • Android
  • iOS

usage

  1. Configure permissions
  2. Check permissions. When calling some apis that require permissions, check whether you have the permissions first
  3. Request permission. If not authorized, these permissions are requested from the user
  4. To deal with the callback

Configure permissions

Android

In the project root directory to open the android/app/SRC/main/AndroidManifest. The XML file, then configure the permissions:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xyz.bczl.flutter.easy_permission_example">
    <! -- Configure permissions here -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
   <application
        android:label="flutter_easy_permission_example"
        android:icon="@mipmap/ic_launcher">
        <! -... -->
   </application>
</manifest>
Copy the code

For a detailed explanation of these constants, go here.

To see how permissions are handled on Android, here’s the full documentation.

iOS

Open the ios/Runner/ info.plist file in the root directory of the project and configure the permissions you need:


      
<! DOCTYPEplist PUBLIC "- / / / / DTD PLIST Apple 1.0 / / EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <! -- Configure permissions here -->
    <key>NSCameraUsageDescription</key>
	<string>Explain to the user why you need this permission</string>

    <! -... -->
</dict>
</plist>
Copy the code

Note that replacing the contents of the
tag gives the user a reason for needing permission.

For a detailed explanation of iOS permissions, you can check it out here.

This plugin wraps a library of LBXPermission for iOS. Integrating with unused permissions libraries in iOS may not be approved by the App Store, so don’t integrate with unused permissions libraries, so you’ll need to do some configuration.

Open the ios/Podfile file and add the following code.

target 'Runner' do
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  # Add the library of permissions you need here
  pod 'LBXPermission/Camera'
end
Copy the code

Libraries you can integrate:

pod 'LBXPermission/Camera'
pod 'LBXPermission/Photo'
pod 'LBXPermission/Contact'
pod 'LBXPermission/Location'
pod 'LBXPermission/Reminder'
pod 'LBXPermission/Calendar'
pod 'LBXPermission/Microphone'
pod 'LBXPermission/Health'
pod 'LBXPermission/Net'
pod 'LBXPermission/Tracking'
pod 'LBXPermission/Notification'
pod 'LBXPermission/Bluetooth'
Copy the code

Once configured, you’ll need to run the install command in your project’s ios directory:

pod install
Copy the code

Check the permissions

const permissions = [Permissions.CAMERA];
const permissionGroup = [PermissionGroup.Camera];

bool ret = await FlutterEasyPermission.has(perms: permissions,permsGroup: permissionGroup);
Copy the code

Android and iOS permissions are so different that it’s hard to handle them all together, so you have to handle them separately. The perms parameter corresponds to the Android permission, and the permsGroup parameter corresponds to the iOS permission. Apps can only run on one platform at a time, so you don’t need to worry about clutter.

Note the relationship between apis and libraries. To check permissions associated with requests, you must integrate the corresponding libraries, as shown in the following table:

PermissionGroup Info.plist Integrated lib
Calendar NSCalendarsUsageDescription LBXPermission/Calendar
Reminders NSRemindersUsageDescription LBXPermission/Reminder
Contacts NSContactsUsageDescription LBXPermission/Contact
Camera NSCameraUsageDescription LBXPermission/Camera
Microphone NSMicrophoneUsageDescription LBXPermission/Microphone
Photos NSPhotoLibraryUsageDescription LBXPermission/Photo
Location NSLocationUsageDescription

NSLocationAlwaysAndWhenInUseUsageDescription

NSLocationWhenInUseUsageDescription
LBXPermission/Location
Notification PermissionGroupNotification LBXPermission/Notification
Bluetooth NSBluetoothAlwaysUsageDescription

NSBluetoothPeripheralUsageDescription
LBXPermission/Bluetooth

Request permission to

FlutterEasyPermission.request(
                    perms: permissions,permsGroup: permissionGroup,rationale:"Test permission requests here");
Copy the code

To deal with the callback

void initState() {
    super.initState(); _easyPermission = FlutterEasyPermission() .. addPermissionCallback( onGranted: (requestCode,perms,perm){ debugPrint("Android Authorized:$perms");
          debugPrint("iOS Authorized:$perm");
        },
        onDenied: (requestCode,perms,perm,isPermanent){
          if(isPermanent){
            FlutterEasyPermission.showAppSettingsDialog(title: "Camera");
          }else{
            debugPrint("Android Deny authorization:$perms");
            debugPrint("iOS Deny authorization:$perm"); }}); }void dispose() {
    _easyPermission.dispose();
    super.dispose();
}
Copy the code

When isPermanent returns true, the system will not pop up the permissions dialog box during the claim limit, so you may need to pop up a dialog box yourself, which will basically tell the user that if you must use this feature, you can go to the system Settings page and re-open the permissions.

On Android, you may also want to implement the onSettingsReturned callback to better handle permission interactions. It is the callback after showAppSettingsDialog is called.

example

For a complete example, seehere.

You can also see examples of its use in Flutter -scankit

Plug-in development

If you are interested in the development of the Flutter plugin, you can go to my website to check out the course full Stack Flutter Development – Advanced. This course provides an exclusive in-depth explanation of all aspects of Flutter plugin development and explains several practical cases (including the development process of this plugin).

Welcome to the public account: the path of programming from 0 to 1