The simplest plug-in for rights management of flutter

simplest plug-in plug rights management


This is a Flutter A plug-in library for dynamic permission processing on , It can make Flutter The developers of the application layer take a very simple API Unified processing of native dynamic permissions . It encapsulates the checking of permissions 、 request , And when permission is permanently denied , Pull up the system setup page properly , Prompt the user to manually open permissions . I can hardly think of a reason to refuse to use it .

Warehouse address : flutter_easy_permission

  • Android
  • iOS

usage

  1. Configure permissions
  2. Check authority . When calling something that needs permission API when , You should first check whether you have the relevant permissions
  3. Request permission . If not authorized , Request these permissions from the user
  4. Handle callbacks

Configure permissions

Android

Open... In the project root directory android/app/src/main/AndroidManifest.xml file , Then configure the required 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 code 

A detailed description of these constants , Please go to here .

To understand Android How to handle permissions on , here There's a complete document .

iOS

Open the ios/Runner/Info.plist file , Configure the permissions you need :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 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 code 

Be careful , Replace <string></string> Content in Tags , Give the user a reason to need permission .

About iOS Detailed explanation of authority , You can check here .

This plug-in encapsulates one for iOS Of LBXPermission library . Integrate iOS Permission library not used in , May not pass app store audit , So don't integrate those unused permission Libraries , So you also need to do some configuration .

open ios/Podfile file , 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 code 

You can integrate Libraries :

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 code 

After configuration , You need to be in the middle of the project ios Run the installation command under the directory :

pod install
 Copy code 

Check authority

const permissions = [Permissions.CAMERA];
const permissionGroup = [PermissionGroup.Camera];
bool ret = await FlutterEasyPermission.has(perms: permissions,permsGroup: permissionGroup);
 Copy code 

because Android and iOS There's a big difference in the authority of , It's hard to deal with them in a unified way , So you have to deal with it separately . Parameters perms The corresponding is Android jurisdiction , Parameters permsGroup The corresponding is iOS jurisdiction .app You can only run on one platform at a time , So you don't have to worry about chaos .

Be careful API And the library , To check the permissions associated with the request , You have to integrate the corresponding Libraries , See the table below :

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

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

Handle callbacks

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 code 

When isPermanent return true when , Indicates that the authorization dialog box will not pop up when the system requests permission , So you may need to pop up a dialog of your own , The main content is to prompt users , If you have to use this feature , You can go to the system settings page to re open the permissions .

stay Android On , You may also need to achieve onSettingsReturned Callback function , To better handle privilege interactions . It is showAppSettingsDialog Callback after being called .

Example

A complete example , see here .

You can also see it in flutter-scankit Examples of use in

Plug-in development

If you are right about Flutter I'm interested in plug-in development content , You can go to my online school to check 《Flutter Full plank development - Advanced 》 Curriculum , Exclusive in-depth analysis of the whole network about Flutter All aspects of plug-in development , The actual combat explains many cases ( Contains the plug-in development process )

Welcome to the official account : The way of programming is from 0 To 1

版权声明
本文为[The way of programming from 0 to 1]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210503144051153p.html

  1. Why did gitlab choose vue.js?
  2. HTTP-RPC: 轻量跨平台REST服务
  3. 继全面采用Node.js以后,PayPal分享大幅度踩坑GraphQL心得 - Mark Stuart
  4. vue组件化开发实战之滚动/轮播的实现
  5. Http-rpc: lightweight cross platform rest Service
  6. Following the full adoption of node.js, PayPal shares a great deal of graphql experience mark Stuart
  7. Implementation of rolling / carousel in Vue component development
  8. CSS是什么?这一篇全解,绝对有你想要的
  9. What is CSS? This is a complete solution, there is absolutely what you want
  10. 04-HTML5常用标签-HTML5极速入门
  11. 04-html5 common tags
  12. WEB前端全套零基础视频教程+软件2021最新编程视频
  13. Web front end full set of zero basic video tutorial + software 2021 latest programming video
  14. 使用Node, Mongo, React, Redux实现Token认证
  15. Using node, Mongo, react and Redux to realize token authentication
  16. 体面编码之CSS和HTML
  17. CSS and HTML for decent coding
  18. 使用Playwright基于多浏览器进行javascript自动化测试的简单教程- Applitools
  19. A simple tutorial for JavaScript automatic testing based on multi browser using playwright - applitools
  20. Minimum distance to target element
  21. 浅谈 React 中的 XSS 攻击
  22. XSS attack in react
  23. 自学前端教程整理,附不容错过的前端100篇文章合集
  24. Self taught front-end tutorial collation, with a collection of 100 front-end articles that can not be missed
  25. 使用OpenTracing跟踪Go中的HTTP请求延迟
  26. Using opentracing to track HTTP request latency in go
  27. Encapsulating databinding allows you to write less than 10000 lines of code
  28. 03-HTML5标签-HTML5极速入门
  29. 03-html5 tag-html5 quick start
  30. LayUI - 极易上手拿来即用的前端 UI 框架
  31. Layui - easy to use front end UI framework
  32. Interpretation of lodash source code (1)
  33. Why is the first parameter of node family callback error?
  34. 报告:JavaScript 开发者达1380 万,C#超越 PHP,Rust 增长最快
  35. Report: Javascript developers reach 13.8 million, C surpasses PHP, and rust grows fastest
  36. 小白前端入门笔记(10),怎么设置网站内部的超链接?
  37. How to set up hyperlinks inside the website?
  38. Using node and socket to realize online chat room
  39. The core competitiveness of Vue: data bidirectional binding
  40. React configuration agent
  41. CSS layout
  42. Application scenario explanation of Vue dynamic component
  43. Redux learning notes 04 -- using multiple reducers to manage data
  44. After three months of typescript writing, what have I learned?
  45. Node family - what is a callback?
  46. React -- a simple implementation of render & create element
  47. JS learning simple usage of jquery
  48. Seamless love
  49. 小白前端入门笔记(12),设置哑链接
  50. Small white front-end entry notes (12), set dumb links
  51. Vue2. X opens composition API and TSX
  52. Interview record and thinking of social recruitment for one and a half years (Alibaba, Tencent, baidu offer)
  53. Flex learning notes
  54. The most essential closure article in the eastern hemisphere
  55. 2021-05-03 hot news
  56. Sword finger offer -- reverse order pair in array (JS Implementation)
  57. Working process of scaffold
  58. Use decorator mode to strengthen your fetch
  59. [JS] scope (Introduction)
  60. Employment information statistics network (interface document)