flutter在pub.flutter-io.cn插件库中有很多的关于权限配置的插件,但是就我个人而言,比较推荐使用permission_handler这个插件。当我们打开 permission_handler 时候,新手小白往往会因为它的官网文档弄得一头雾水。权限配置往往涉及到 Android 和 iOS 两个方向的相关知识,有可能大多数人就只会 Android 或 iOS 某一端知识,在配置权限时会出现屡屡不生效的问题,接下来我从零到一教你如何配置吧。
flutter版本号:3.0.0
dart版本号:2.12.0
插件:permission_handler
1、使用的插件
permission_handler: ^9.0.2
flutter_easyloading: ^3.0.5
2、配置权限
Android 端:
(1)将以下内容添加到 gradle.properties 文件中:(针对androidX及以上的版本)
android.useAndroidX=true
android.enableJetifier=true
(2)将以下内容添加到 AndroidManifest.xml 文件中:(针对androidX及以上的版本)
android:hardwareAccelerated="true"
(3)按需在 AndroidManifest.xml 添加本项目需要用到的权限(本次以保存图片到本地相册举例)
iOS 端:
(1)将以下内容添加到您的 Podfile 文件中:
post_install do |installer|
installer.pods_project.targets.each do |target|
... # Here are some configurations automatically generated by flutter
# Start of the permission_handler configuration
target.build_configurations.each do |config|
# 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_apple/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 of the permission_handler configuration
end
end
(2)删除要使用的权限前面的字符 # 。例如,如果您需要访问相册,请确保代码如下所示:
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
(3)Info.plist 添加
NSPhotoLibraryAddUsageDescription
Please allow the APP to save photos to the album
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:permission_handler/permission_handler.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State createState() => _HomePageState();
}
class _HomePageState extends State {
/// 动态申请权限,需要区分android和ios,很多时候它两配置权限时各自的名称不同
/// 此处以保存图片需要的配置为例
Future requestPermission() async {
late PermissionStatus status;
// 1、读取系统权限的弹框
if (Platform.isIOS) {
status = await Permission.photosAddOnly.request();
} else {
status = await Permission.storage.request();
}
// 2、假如你点not allow后,下次点击不会在出现系统权限的弹框(系统权限的弹框只会出现一次),
// 这时候需要你自己写一个弹框,然后去打开app权限的页面
if (status != PermissionStatus.granted) {
showCupertinoDialog(
context: context,
builder: (context) {
return CupertinoAlertDialog(
title: const Text('You need to grant album permissions'),
content: const Text(
'Please go to your mobile phone to set the permission to open the corresponding album'),
actions: [
CupertinoDialogAction(
child: const Text('cancle'),
onPressed: () {
Navigator.pop(context);
},
),
CupertinoDialogAction(
child: const Text('confirm'),
onPressed: () {
Navigator.pop(context);
// 打开手机上该app权限的页面
openAppSettings();
},
),
],
);
});
} else {
return true;
}
return false;
}
// 保存网络图片
saveNetworkImg(String imgUrl) async {
// Todo
// 处理保存网络图片的逻辑
EasyLoading.showToast('处理保存网络图片的逻辑');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('home'),
),
floatingActionButton: FloatingActionButton(
onPressed: () async{
// 使用
// 首先判断是否有权限,没权限就不执行了
bool permission = await requestPermission();
permission ? saveNetworkImg('imgUrl') : null;
},
child: const Icon(Icons.adb),
),
);
}
}
Android:
iOS: