Flutter permission_handler Authority Plug in Explanation
- 2021-11-30 01:37:18
- OfStack
Compilation environment: Flutter version v1.12. hotfix9 dart SDK: 2.7. 2
1 Introduced in pubspec. yaml:
# Permissions
permission_handler: ^3.2.0
info. plist configuration in ios (used according to permissions):
<!-- Permission options for the `location` group -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need location when in use</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Always and when in use!</string>
<key>NSLocationUsageDescription</key>
<string>Older devices need location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Can I haz location always?</string>
<!-- Permission options for the `mediaLibrary` group -->
<key>NSAppleMusicUsageDescription</key>
<string>Music!</string>
<key>kTCCServiceMediaLibrary</key>
<string>media</string>
<!-- Permission options for the `calendar` group -->
<key>NSCalendarsUsageDescription</key>
<string>Calendars</string>
<!-- Permission options for the `camera` group -->
<key>NSCameraUsageDescription</key>
<string>camera</string>
<!-- Permission options for the `contacts` group -->
<key>NSContactsUsageDescription</key>
<string>contacts</string>
<!-- Permission options for the `microphone` group -->
<key>NSMicrophoneUsageDescription</key>
<string>microphone</string>
<!-- Permission options for the `speech` group -->
<key>NSSpeechRecognitionUsageDescription</key>
<string>speech</string>
<!-- Permission options for the `sensors` group -->
<key>NSMotionUsageDescription</key>
<string>motion</string>
<!-- Permission options for the `photos` group -->
<key>NSPhotoLibraryUsageDescription</key>
<string>photos</string>
<!-- Permission options for the `reminder` group -->
<key>NSRemindersUsageDescription</key>
<string>reminders</string>
2 code for specific use:
void chosePhoto(int index) async {
PermissionHandler().requestPermissions(
[PermissionGroup.photos, PermissionGroup.camera]).then((map) {
if (map[PermissionGroup.photos] == PermissionStatus.granted ||
map[PermissionGroup.camera] == PermissionStatus.granted) {
chosePhotoFromPhone(context, index);
}
});
}
3 Summary:
In the process of use, androidX or Swift language adaptation may occur, which needs to be modified according to the specific situation.