20.2 设备动态权限查询

20.2 设备动态权限查询

img img

实现步骤:


第 1 步:安装插件 permission_handler

pubspec.yaml

1
2
3
dependencies:
# permission 权限
permission_handler: 9.2.0

第 2 步:ios

ios/Podfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)

#########################################################################
## permissions

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

在需要的权限前去掉注释

ios/Runner/Info.plist

1
2
3
4
5
6
<key>NSCameraUsageDescription</key>
<string>Need to access the camera to take photos and videos to share with friends.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Requires access to the microphone to shoot video and voice to share with friends.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Requires access to the photo library to read videos and photos to share with friends.</string>

NSCameraUsageDescription 启用相机说明
NSMicrophoneUsageDescription 启用麦克风说明
NSPhotoLibraryUsageDescription 启用相册说明

第 3 步:android

android/app/build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
android {
// compileSdkVersion flutter.compileSdkVersion
compileSdkVersion 31

...

defaultConfig {
...

minSdkVersion 21
targetSdkVersion 30

...
}

compileSdkVersion 编译 sdk 版本
minSdkVersion 最小的 sdk 版本
targetSdkVersion 目标机器的 sdk 版本,关系到设备特性的开启

android/app/src/main/AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<manifest>
<application></application>

<!--
Internet permissions do not affect the `permission_handler` plugin, but are required if your app needs access to
the internet.
-->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- Permissions options for the `storage` group -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- Permissions options for the `camera` group -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

<!-- Permissions options for the `microphone` or `speech` group -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>

第 4 步:动态授权 Privilege

lib/common/utils/privilege.dart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:permission_handler/permission_handler.dart';

import 'index.dart';

/// 授权返回
class PrivilegeStatus {
final bool result;
final String message;

PrivilegeStatus({required this.result, required this.message});
}

/// 动态授权
class Privilege {
/// 是否有相册权限
static Future<PrivilegeStatus> photos() async {
var value = false;
if (GetPlatform.isIOS) {
var statuses = await [Permission.photos].request();
value = statuses[Permission.photos] == PermissionStatus.granted;
}
if (GetPlatform.isAndroid) {
var statuses = await [Permission.storage].request();
value = statuses[Permission.storage] == PermissionStatus.granted;
}
return PrivilegeStatus(
result: value,
message: value ? 'ok' : 'Please open photo gallery access',
);
}

/// 是否有相机权限
static Future<PrivilegeStatus> camera() async {
var value = false;
var permissions = <Permission>[];
permissions.addAll([Permission.camera, Permission.microphone]);
if (GetPlatform.isIOS) {
permissions.add(Permission.photos);
}
if (GetPlatform.isAndroid) {
permissions.add(Permission.storage);
}
var statuses = await permissions.request();
value = statuses[Permission.camera] == PermissionStatus.granted &&
statuses[Permission.microphone] == PermissionStatus.granted;
if (GetPlatform.isIOS) {
value = statuses[Permission.photos] == PermissionStatus.granted;
}
if (GetPlatform.isAndroid) {
value = statuses[Permission.storage] == PermissionStatus.granted;
}
return PrivilegeStatus(
result: value,
message: value ? 'ok' : 'Please turn on camera and microphone access',
);
}

/// 是否有麦克风权限
static Future<PrivilegeStatus> microphone() async {
var value = false;
var statuses = await [Permission.microphone].request();
value = statuses[Permission.microphone] == PermissionStatus.granted;
return PrivilegeStatus(
result: value,
message: value ? 'ok' : 'Please turn on microphone access',
);
}

/// 是否有存储权限
static Future<bool> storage() async {
bool value = false;
String text = '';
if (GetPlatform.isAndroid) {
value = await Permission.storage.request().isGranted;
text =
'Please allow access to storage space, settings will be opened soon';
}
if (GetPlatform.isIOS) value = true;
if (!value) {
Loading.toast(text);
await Future.delayed(const Duration(seconds: 2));
openSettings();
}
return value;
}

/// 打开系统设置
static Future<bool> openSettings() async {
return await openAppSettings();
}
}

提交代码到 git