permission_plus 0.1.0
permission_plus: ^0.1.0 copied to clipboard
A Flutter plugin for requesting and checking permissions.
permission_plus #
A unified, cross-platform Flutter plugin for requesting and checking permissions on Android, iOS, macOS, Web, Windows, and Linux.
Features #
- Check permission status (
granted,denied,restricted,permanentlyDenied,limited,provisional, andnotDetermined). - Request a single permission or multiple permissions at once.
- Open the platform's app settings page.
- Determine if a rationale should be shown before requesting a permission (Android only).
- Fetch location accuracy (precise vs reduced).
- Type-safe implementation leveraging Pigeon-generated host APIs.
Supported Platforms #
- Android: API 21+
- iOS: iOS 12.0+
- macOS: macOS 10.14+
- Web: Chrome, Firefox, Safari (uses the Web Permissions API)
- Windows: Windows 10+ (Win32 / WinRT)
- Linux: Any modern Linux distribution (returns
grantedfor desktop apps)
Setup #
Android #
Add the required permissions to your android/app/src/main/AndroidManifest.xml file. For example, to use the Camera and Location permissions:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Add permissions here -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
iOS #
Add the corresponding usage descriptions to your ios/Runner/Info.plist. For example, for Camera and Location:
<key>NSCameraUsageDescription</key>
<string>This app needs camera access to take photos.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs location access to find nearby places.</string>
macOS #
Add the usage descriptions to your macos/Runner/Info.plist (same as iOS). Additionally, if your macOS app is sandboxed (which is the default), you must add entitlements to macos/Runner/DebugProfile.entitlements and Release.entitlements.
For example, to access the camera and location:
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.personal-information.location</key>
<true/>
Web, Windows, and Linux #
No special setup is required.
- On the Web, the browser handles permission prompts automatically using the standard Web Permissions API.
- On Windows, Win32 apps have unrestricted access to most resources. Permissions like Camera and Location will check Windows Privacy Settings.
- On Linux, standard desktop applications have unrestricted access to the user's hardware and files, so all permissions will return
granted.
Usage #
1. Check a permission status #
import 'package:permission_plus/permission_plus.dart';
Future<void> checkCamera() async {
final status = await PermissionPlus.checkPermission(PermissionType.camera);
switch (status) {
case PermissionStatus.granted:
print('Camera access is granted.');
break;
case PermissionStatus.denied:
print('Camera access is denied.');
break;
case PermissionStatus.permanentlyDenied:
print('Camera access is permanently denied.');
break;
case PermissionStatus.restricted:
print('Camera access is restricted (e.g. parental controls).');
break;
case PermissionStatus.limited:
print('Camera access is limited.');
break;
case PermissionStatus.provisional:
print('Camera access is provisional.');
break;
case PermissionStatus.notDetermined:
print('Camera access has not been determined yet.');
break;
}
}
2. Request a permission #
import 'package:permission_plus/permission_plus.dart';
Future<void> requestCamera() async {
final status = await PermissionPlus.requestPermission(PermissionType.camera);
if (status == PermissionStatus.granted) {
print('Camera permission granted');
} else if (status == PermissionStatus.permanentlyDenied) {
print('Permission permanently denied. Please enable it in settings.');
await PermissionPlus.openSettings();
}
}
3. Request multiple permissions #
import 'package:permission_plus/permission_plus.dart';
Future<void> requestMultiple() async {
final statuses = await PermissionPlus.requestPermissions([
PermissionType.camera,
PermissionType.location,
]);
if (statuses[PermissionType.camera] == PermissionStatus.granted) {
print('Camera granted');
}
}
4. Open App Settings #
If a permission is permanentlyDenied, you can guide the user to the platform's settings page so they can enable it manually.
import 'package:permission_plus/permission_plus.dart';
Future<void> openSettings() async {
final opened = await PermissionPlus.openSettings();
if (opened) {
print('Successfully opened settings.');
} else {
print('Could not open settings.');
}
}
Contributing #
Contributions are welcome! Please open an issue or pull request on the GitHub repository.