I use the permission_handler package. On Android the request dialog never shows and on iOS the app crashes when opening the camera. What platform configuration is required and how should the request flow look?
#flutter#permissions#ios#android
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
I use the permission_handler package. On Android the request dialog never shows and on iOS the app crashes when opening the camera. What platform configuration is required and how should the request flow look?
#flutter#permissions#ios#android
1. Declare permissions on each platform
Android, android/app/src/main/AndroidManifest.xml (above <application>):
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
iOS, ios/Runner/Info.plist (a missing usage string crashes the app on access):
<key>NSCameraUsageDescription</key>
<string>We use the camera to scan barcodes.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to show nearby stores.</string>
2. iOS: enable the permission in the Podfile
permission_handler compiles only the permissions you enable. In ios/Podfile, inside post_install, set the macros, for example:
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
'PERMISSION_CAMERA=1',
'PERMISSION_LOCATION=1',
]
Without this, requests return denied without ever showing a prompt. Check the package README for the macro names of your version.
3. Request flow in Dart
Future<bool> ensureCamera(BuildContext context) async {
var status = await Permission.camera.status;
if (status.isGranted) return true;
status = await Permission.camera.request();
if (status.isGranted) return true;
if (status.isPermanentlyDenied) {
// the OS will no longer show the dialog; send the user to settings
await openAppSettings();
}
return false;
}
Good practice
denied, permanentlyDenied and (iOS) restricted separately.POST_NOTIFICATIONS; photo access has granular media permissions.See a full pattern in the push permission flow snippet.