Search code examples
iosflutterpermissions

Flutter app stopped asking for gallery and camera access


I have recently done some refactoring of my Flutter iOS app, mainly separating code from main.dart and moving to separate files.

After this refactoring, the app suddenly stopped asking the user for permission to use the camera or the gallery, and am unable to access these respective resources.

I am running XCode 15, and iOS 17

Steps I have tried to resolve this issue.

  1. Ensured that the flutter package permission_handler is updated (v11.0.1)
  2. Double-checked the info.pblist contains the correct keys:
        <key>NSCameraUsageDescription</key>
    <string>We need access to your camera for taking pictures.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to your photo library for selecting pictures.</string>
  1. Updated pod (1.13.0)

  2. Updated homebrew (4.1.15)

  3. Updated Flutter and its tools: Flutter 3.13.6 • channel stable • https://github.com/flutter/flutter.git Framework • revision ead455963c (2 weeks ago) • 2023-09-26 18:28:17 -0700 Engine • revision a794cf2681 Tools • Dart 3.1.3 • DevTools 2.25.0

  4. Ran following commands:

  • flutter clean
  • flutter pub get
  • rm -rf Pods/ Podfile.lock
  • rm -rf /ios
  • flutter build ios
  1. Reinstalled the app (many times) both physical and simulation env.

Relevant code snippet:

  File? _image;
  UploadState _uploadState = UploadState.idle;

  Future<void> _checkPermissionsAndPickImage(
      ImageSource source, BuildContext context) async {
    Permission permission =
        (source == ImageSource.camera) ? Permission.camera : Permission.photos;

    PermissionStatus status = await permission.request();

    if (status.isGranted) {
      _pickImage(source);
    } else if (status.isPermanentlyDenied) {
      _showPermissionDeniedDialog(context);
    } else {
      print('Permission not granted. Cannot access $source.');
    }
  }

Here, _showPermissionDeniedDialog will notify the user that they have permanently blocked the app from accessing the camera and the gallery, and I guide the users to settings to change this.

What I find strange is that I cannot find the app in the settings to change the permissions...


Solution

  • RTFM solved my issue. Changing my post_install in the Podfile to include the necessary permissions solved the issue:

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

      #  Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
    
    
        ## dart: PermissionGroup.camera
        'PERMISSION_CAMERA=1',
    
        ## dart: PermissionGroup.photos
        'PERMISSION_PHOTOS=1',
      ]
    
    end 
    # Permissions   end end
    

    Why I needed to do this after code refactoring is beyond me.