flutter ios photos permission always return permission denied permanantly

Viewed 2456

I'm using flutter to create an app for both ios and android. I need photos and storage permission for ios and android to access photos to update user profile pictures. I used the permission_handler package to request permissions. And this works well in android but not in ios.

<key>NSPhotoLibraryUsageDescription</key>
<string>We need Photos access to allow you to update Profile Picture.</string>
var permission = Platform.isAndroid
        ? Permission.storage
        : Permission.photos;

var permissionStatus = await permission.request();

print("isGranted: " +
        permissionStatus.isGranted.toString() +
        " isDenied: " +
        permissionStatus.isDenied.toString() +
        " isLimited: " +
        permissionStatus.isLimited.toString() +
        " isRestricted: " +
        permissionStatus.isRestricted.toString() +
        " isPermanentlyDenied: " +
        permissionStatus.isPermanentlyDenied.toString());


flutter: isGranted: false isDenied: false isLimited: false isRestricted: false isPermanentlyDenied: true

I tried to uninstall and reinstall the app on the simulator (iPhone 12pro max) but still not able to request permission (show request popup).

I tried this, this, this and this. but the issue remains.

3 Answers

The behavior on iOS for the Permissions package has changed to default all permissions to denied. You have to update the PList and turn on the permissions you wish to use within your application for it to work.

First copy and past below code into your podfile. Than set 'PERMISSION_PHOTOS=1', if you dont want use another permission you can delete or set it =0 (exp 'PERMISSION_CAMERA=0', ).

    post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    target.build_configurations.each do |config|
          # You can remove unused permissions here
          # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
            '$(inherited)',
            ## dart: PermissionGroup.calendar

            ## dart: PermissionGroup.reminders

            ## dart: PermissionGroup.contacts

            ## dart: PermissionGroup.camera
            'PERMISSION_CAMERA=1',
            ## dart: PermissionGroup.microphone
            'PERMISSION_MICROPHONE=1',
            ## dart: PermissionGroup.speech
            'PERMISSION_SPEECH_RECOGNIZER=1',
            ## dart: PermissionGroup.photos
            'PERMISSION_PHOTOS=1',
            ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]

            ## dart: PermissionGroup.notification

            ## dart: PermissionGroup.mediaLibrary
            'PERMISSION_MEDIA_LIBRARY=1',
            ## dart: PermissionGroup.sensors

            ## dart: PermissionGroup.bluetooth

            ## dart: PermissionGroup.appTrackingTransparency
            'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
            ## dart: PermissionGroup.criticalAlerts
            'PERMISSION_SENSORS=1'
          ]
        end
  end
end

Than add this into Info.plist file

<key>NSPhotoLibraryUsageDescription</key>
 <string>We need Photos access to allow you to update Profile Picture.</string>

Finally use permission_handler plugin into your project and add this code where you want to ask permission.

 var permissionStatus = await permission.request();

Use latest permission_handler: package. Add photo permission to your Info.plist file.

 <!-- Permission options for the `photos` group -->
    <key>NSPhotoLibraryUsageDescription</key>
    <string>photos</string>

Add the following to your Podfile file:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    ... # Here are some configurations automatically generated by flutter

    # Start of the permission_handler configuration
    target.build_configurations.each do |config|

      # You can enable the permissions needed here. For example to enable camera
      # permission, just remove the `#` character in front so it looks like this:
      #
      # ## dart: PermissionGroup.camera
      # 'PERMISSION_CAMERA=1'
      #
      #  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.calendar
        # 'PERMISSION_EVENTS=1',

        ## dart: PermissionGroup.reminders
        # 'PERMISSION_REMINDERS=1',

        ## dart: PermissionGroup.contacts
        # 'PERMISSION_CONTACTS=1',

        ## dart: PermissionGroup.camera
        # 'PERMISSION_CAMERA=1',

        ## dart: PermissionGroup.microphone
        # 'PERMISSION_MICROPHONE=1',

        ## dart: PermissionGroup.speech
        # 'PERMISSION_SPEECH_RECOGNIZER=1',

        ## dart: PermissionGroup.photos
         'PERMISSION_PHOTOS=1',

        ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
        # 'PERMISSION_LOCATION=1',

        ## dart: PermissionGroup.notification
        # 'PERMISSION_NOTIFICATIONS=1',

        ## dart: PermissionGroup.mediaLibrary
        # 'PERMISSION_MEDIA_LIBRARY=1',

        ## dart: PermissionGroup.sensors
        # 'PERMISSION_SENSORS=1',   

        ## dart: PermissionGroup.bluetooth
        # 'PERMISSION_BLUETOOTH=1',

        ## dart: PermissionGroup.appTrackingTransparency
        # 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',

        ## dart: PermissionGroup.criticalAlerts
        # 'PERMISSION_CRITICAL_ALERTS=1'
      ]

    end 
    # End of the permission_handler configuration
  end
end

if you need to add more permission. Remove the # character in front of the permission you do want to use. for example

   ## dart: PermissionGroup.photos
             'PERMISSION_PHOTOS=1',

in flutter, request var status = await Permission.photos.request(); if (status.isDenied) { // We didn't ask for permission yet or the permission has been denied before but not permanently. }

finally Clean & Rebuild

Related