Flutter iOS - Not asking for Camera permission
Asked Answered
D

4

7

Implemented the example from the camera package here:

https://pub.dev/packages/camera/example

While testing on the physical device (running iOS 16), the app builds and runs fine, however the phone does not ask for any permissions to access the camera or microphone.

The following code has been added to ios/Runner/Info.plist

<key>NSCameraUsageDescription</key>
<string>Testing the Camera integration.</string>
<key>NSMicrophoneUsageDescription</key>
<string>To add sounds to the videos you record.</string>

The iOS Deployment Target has been set to iOS 11.0

Note: I can assure you that the app has not been granted these permissions already because:

  1. It is not showing up in the app settings
  2. The app is not listed in Settings>Privacy & Security>Camera

Am I missing something?

Update:

Created a clean projects to test out this example code, Implemented the permission_handler to force the permissions (based on recommendation from @cenk-yagmur).

The permissions window now comes up, however the example code still doesn't work.

This leads me to believe it is either:

  1. The camera package doesn't work on iOS16
  2. The example code is wrong.

I'm more inclined towards 2. Will be doing a custom integration and test if that fixes the issue.

Daiquiri answered 1/12, 2022 at 10:25 Comment(8)
Check your app permissions from settings. Maybe you allowed it sometime ago.Burnejones
@Md.KamrulAmin I have checked and there is no camera option in the settings, and if I go to privacy and camera, then the app is not listed there either.Daiquiri
By any chance are you using IOS simulator?Burnejones
@Md.KamrulAmin nope, testing on a physical device running iOS 16Daiquiri
this feels like a pretty weird problem. Please check your app in a device running a lower OS than 16. If you see it working as desired in lower OS then you can be sure that this package does not support IOS 16 yet.Burnejones
have you uninstall and install the app? may be this can work.Heavyduty
I'm having the same issue all of a sudden. Never happened to me before with an iOS app ...Contrabandist
I am also having same issue on mysids, if anyone having solution then please suggest...Wideangle
I
10

You can add this code to the PodFile and try again.

Don't forget to clean, pub get and pod install.

Remember just turn off the comment line for the properties you are using and set the value to 1.

   post_install do |installer|
      installer.pods_project.targets.each do |target|
        flutter_additional_ios_build_settings(target)
        target.build_configurations.each do |config|
                config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
                   '$(inherited)',
    
                   ## dart: PermissionGroup.calendar
                   ##'PERMISSION_EVENTS=1',
    
                   ## dart: PermissionGroup.reminders
                   #'PERMISSION_REMINDERS=0',
    
                   ## dart: PermissionGroup.contacts
                   # 'PERMISSION_CONTACTS=0',
    
                   ## dart: PermissionGroup.camera
                   'PERMISSION_CAMERA=1',
    
                   ## dart: PermissionGroup.microphone
                   'PERMISSION_MICROPHONE=1',
    
                   ## dart: PermissionGroup.speech
                   #'PERMISSION_SPEECH_RECOGNIZER=0'
    
                   ## dart: PermissionGroup.photos
                   'PERMISSION_PHOTOS=1',
    
                   ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
                   'PERMISSION_LOCATION=1',
    
                   ## dart: PermissionGroup.notification
                   'PERMISSION_NOTIFICATIONS=1',
    
                   ## dart: PermissionGroup.appTrackingTransparency
                    ##'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
    
                   ## dart: PermissionGroup.mediaLibrary
                   ##'PERMISSION_MEDIA_LIBRARY=1'
    
                   ## dart: PermissionGroup.sensors
                   #'PERMISSION_SENSORS=0'
                 ]
        end
      end
    end

My Photos and Camera permissions function code:

Future<bool?> _checkPermission(BuildContext context) async {
  if (Platform.isAndroid) {
    Map<Permission, PermissionStatus> statues = await [Permission.camera, Permission.photos].request();
    PermissionStatus? statusCamera = statues[Permission.camera];

    PermissionStatus? statusPhotos = statues[Permission.photos];
    bool isGranted = statusCamera == PermissionStatus.granted && statusPhotos == PermissionStatus.granted;
    if (isGranted) {
      return true;
    }
    bool isPermanentlyDenied = statusCamera == PermissionStatus.permanentlyDenied || statusPhotos == PermissionStatus.permanentlyDenied;
    if (isPermanentlyDenied) {
      return false;
    }
  } else {
    Map<Permission, PermissionStatus> statues = await [Permission.camera, Permission.storage, Permission.photos].request();
    PermissionStatus? statusCamera = statues[Permission.camera];
    PermissionStatus? statusStorage = statues[Permission.storage];
    PermissionStatus? statusPhotos = statues[Permission.photos];
    bool isGranted = statusCamera == PermissionStatus.granted && statusStorage == PermissionStatus.granted && statusPhotos == PermissionStatus.granted;
    if (isGranted) {
      return true;
    }
    bool isPermanentlyDenied = statusCamera == PermissionStatus.permanentlyDenied || statusStorage == PermissionStatus.permanentlyDenied || statusPhotos == PermissionStatus.permanentlyDenied;
    if (isPermanentlyDenied) {
      return false;
    }
  }
}
Interplanetary answered 1/12, 2022 at 11:18 Comment(5)
Tried this and unfortunately it didn't make any difference.Daiquiri
If the permission is permanently denied, the permission dialog will not open again. You know that right?Interplanetary
yes of course. As I had mentioned in my original question those settings were not granted, nor were they even present in the settings.Daiquiri
Also tried this update to the podfile, and have it as an implementation in one of my other apps, although it made no difference this time.Contrabandist
Works for me. @TJ In case of permaentlyDenied status you can call the openAppSettings() function which will open the App Setting where the User can grant the permission.Ehf
K
0

https://pub.dev/packages/permission_handler

To avoid problems, have it checked before accessing the camera to see if it has permission.

Map<Permission, PermissionStatus> statuses = await [
  Permission.camera,
  Permission.microphone,
].request();
Koslo answered 1/12, 2022 at 10:44 Comment(4)
I haven't implemented the permission_handler, simply because the camera package and the flutter docs, don't explicitly ask for the permission_handler package.Daiquiri
I had the same problem and couldn't find any other solution.Koslo
Yes, so I implemented this package to see if it triggers the permissions. And it does! But strangely the example code still doesn't work, it still returns a null list of cameras. I even did a print of the list of cameras and it seems to be showing a list, however now I'm suspecting that the example code itself is flawed.Daiquiri
cameras = await availableCameras(); Doesn't it throw an error when you import it into the try catch blog after getting the permissions?Koslo
L
0

For me what worked was to remove the permission_handler package all together and rely on the exceptions the camera package throws. I hope this helps

Laundryman answered 11/6, 2023 at 17:50 Comment(0)
A
-1

If the system not asking the permission than it was granted before

Aurlie answered 1/12, 2022 at 10:37 Comment(1)
Then it would show up in the app settings OR the Settings>Privacy & Security>Camera However it is not showing there.Daiquiri

© 2022 - 2024 — McMap. All rights reserved.