优秀的编程知识分享平台

网站首页 > 技术文章 正文

Flutter里关于权限的故事(flutter中文网)

nanyue 2024-08-13 07:52:17 技术文章 12 ℃


木辛老师来了,本节课咱们一起来看看如何在Flutter中使用Permission吧!

请点击右上角“关注”按钮关注我们哟:跟着木辛老师学习Flutter基础编程知识,变身快乐的编程达人吧~


如果你想获取设备中的某些敏感的资源或者某一些特殊功能,那么你需要向用户请求并获得用户的授权许可。能不能找到一个很好用,还非常简单的插件捏?

这个插件需要通吃Android和iOS两个平台,具有统一的API可以直接使用。

那么,这样的插件是否存在捏?

非常幸运,它确实存在!

这个插件叫做Permission Handler,通过今天的文章你肯定能够熟练地用上它啦!


第一步,装它,装它

使用它就要先安装它,通过修改pubspec.yaml文件,添加如下代码:

dependencies:
	permission_handler: ^8.1.4+2

添加完毕之后需要使用命令:

flutter pub get

进行插件的同步。

第二步,需要配置一下

对于两个手机操作系统:Android和iOS,如果需要使用权限的话,需要针对每一个系统进行设置。

来看看Android的

最新的版本需要AndroidX的支持哟!所以,你需要先切换到AndroidX模式下,修改 gradle.properties文件:

android.useAndroidX=true
android.enableJetifier=true

同时,还需要保证android/app/build.gradle这个文件中的compileSdkVersion不能小于30:

android {
	compileSdkVersion 30
	...
}

再瞅瞅iOS的

对于iOS,需要将权限添加到文件 Info.plist中,permission_handler这个插件需要依赖macros来控制是否打开某一个权限。

你必须要将所有用于当前项目的权限都列出来,并且填入到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

用到哪个权限就删除它前边的那个#吧。

举个例子,比如你需要使用日历权限,那么就需要这么修改

## dart: PermissionGroup.calendar
'PERMISSION_EVENTS=1',

一定要记得,如果不需要某个权限描述的时候,一定要在Info.plist文件中删除掉。比如相机,就需要删除’NSCameraUsageDescription’。

第三步,写个代码吧

使用之前先导入

import 'package:permission_handler/permission_handler.dart';

之后,需要判断权限的状态,它一共有这么几个状态:授权、拒绝、限制或者永久拒绝。

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.
}

然后,请求权限啦,可以使用request()函数实现:

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]);

如果这个时候,你突然发现权限有相关的服务,比如说:位置或者加速度传感器权限,那么还需要再劳烦你检查这些服务是否启用还是处于禁用状态。

if (await Permission.locationWhenInUse.serviceStatus.isEnabled) { // Use location.}

so,你可以借助如下代码打开app的设置页面

if (await Permission.speech.isPermanentlyDenied) {
 // The user opted to never again see the permission request dialog for this
 // app. The only way to change the permission's status now is to let the
 // user manually enable it in the system settings.
 openAppSettings();
}

针对Android,你可以显示一个授权理由:

bool isShown = await Permission.contacts.shouldShowRequestRationale;

如此,你应该就可以开开心心地使用Permission组件库啦!

请大家关注木辛老师的课程哟,获取更多编程知识和编程技巧。接下来,木辛老师和大家一步一步地学习Flutter知识吧。

快乐编程,快乐成长!

咱们下节课再见,88~

最近发表
标签列表