Flutter权限申请
Posted Billy_Zuo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter权限申请相关的知识,希望对你有一定的参考价值。
一、引入permission_handler库
在pubspec.yaml文件中引入permission_handler库
permission_handler: ^9.0.2
permission_handler库地址:https://pub.flutter-io.cn/packages/permission_handler
二、android ios中添加相应权限
Android
1、将以下内容添加到“gradle.properties”文件中:(针对androidX及以上的版本)
android.useAndroidX=true
android.enableJetifier=true
2、确保"android/app/build.gradle"的 compileSdkVersion到31:
android
compileSdkVersion 31
...
3、在“AndroidManifest.xml”添加本项目需要用到的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
ios
1、Podfile 文件添加如下内容:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
... # Here are some configurations automatically generated by flutter
# You can enable the permissions needed here. For example to enable camera
# permission, just remove the `#` character in front so it looks like this:
#
# ## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1'
#
# Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
# 'PERMISSION_EVENTS=1',
## dart: PermissionGroup.reminders
# 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
# 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
# 'PERMISSION_LOCATION=1',
## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
# 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
# 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_CRITICAL_ALERTS=1'
]
end
end
end
2、删除要使用的权限前面的字符。例如,如果您需要访问相册,请确保代码如下所示:
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
3、Info.plist 添加
<!-- 保存图片权限 -->
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Please allow the APP to save photos to the album</string>
4、Clean & Rebuild
三、申请权限
1、申请单个权限:
var status = await Permission.camera.status;
if (status.isDenied)
// We didn't ask for permission yet or the permission has been denied before but not permanently.
// 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.
2、申请多个权限:
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]);
3、granted 通过,denied 被拒绝,permanentlyDenied 拒绝且不在提示,权限被拒绝时,跳转手机设置页面让用户自行开启权限
openAppSettings();
调用前需要先导包
import 'package:permission_handler/permission_handler.dart';
四、示例
///请求录音相机权限
Future<bool> getMicrophonePermission() async
// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
Permission.microphone,
Permission.camera,
].request();
//granted 通过,denied 被拒绝,permanentlyDenied 拒绝且不在提示
if (statuses[Permission.microphone].isGranted &&
statuses[Permission.camera].isGranted)
return true;
return false;
getMicrophonePermission()
.then((value)
if (value) 有权限
else//没有权限
openAppSettings();//打开app系统设置
);
以上是关于Flutter权限申请的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 学习 之 权限管理 permission_handler 9.2.0
请问,我的androi studio中动态申请权限,Manifest.permission.WRITE_EXTERNAL_STORAGE