Flutter permission_handler package not opening app settings when permission is permanently denied

1,726

Solution 1

replace !galleryAccessStatus.isGranted with galleryAccessStatus != PermissionStatus.granted and status.isPermanentlyDenied with status != PermissionStatus.granted

openPhotoGallery() async {
    var galleryAccessStatus = await Permission.mediaLibrary.status;

    if(galleryAccessStatus != PermissionStatus.granted) { //here
      var status = await Permission.mediaLibrary.request();

      if(status != PermissionStatus.granted) {  //here
        await openAppSettings();
      }
    }
  }

Solution 2

I believe this is the best solution I stumbled across. This is the onPressed function of my button that opens the app settings.

onPressed: () async {
                      await openAppSettings();
                      BasicMessageChannel<String?> lifecycleChannel = SystemChannels.lifecycle;
                      lifecycleChannel.setMessageHandler((msg) async {
                        if (msg!.endsWith("resumed")) {
                          lifecycleChannel.setMessageHandler(null);
                          List permissionslist = [
                            await Permission.locationWhenInUse.isGranted,
                            await Permission.bluetoothConnect.isGranted,
                            await Permission.bluetoothScan.isGranted,
                            await Permission.storage.isGranted
                          ];
                          if (permissionslist.firstWhereOrNull((element) => element != true) ==
                              null) {
                            setState(() => arePermissionsGiven = true);
                          }
                          else {
                            setState(() => arePermissionsGiven = false);
                          }
                        }
                        return Future(() => null);
                      });
                    }
Share:
1,726
Sumchans
Author by

Sumchans

Updated on January 01, 2023

Comments

  • Sumchans
    Sumchans about 1 year

    Have been working on the permission_handler package for sometime. Trying to figure out the app settings page on ios or android when user permanently denies access for the app.

    My app to begin with needs access to the media library and the camera. Experienced flutter devs please explain in detail if possible. I am a newbie to flutter development and newbie the whole mobile application development as well.

    Here is podfile on ios

    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=0',
    
                   ## dart: PermissionGroup.reminders
                   'PERMISSION_REMINDERS=0',
    
                   ## dart: PermissionGroup.contacts
                   # 'PERMISSION_CONTACTS=0',
    
                   ## dart: PermissionGroup.camera
                   # 'PERMISSION_CAMERA=1',
    
                   ## dart: PermissionGroup.microphone
                   # 'PERMISSION_MICROPHONE=0',
    
                   ## dart: PermissionGroup.speech
                   'PERMISSION_SPEECH_RECOGNIZER=0',
    
                   ## dart: PermissionGroup.photos
                   # 'PERMISSION_PHOTOS=1',
    
                   ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
                   'PERMISSION_LOCATION=0',
    
                   ## dart: PermissionGroup.notification
                   # 'PERMISSION_NOTIFICATIONS=0',
    
                   ## dart: PermissionGroup.mediaLibrary
                   # 'PERMISSION_MEDIA_LIBRARY=1',
    
                   ## dart: PermissionGroup.sensors
                    'PERMISSION_SENSORS=0'
                 ]
        end
      end
    end
    

    Here is the info.plist-

    <key>NSPhotoLibraryUsageDescription</key>
        <string>This app requires access to your photo gallery to view or save images</string>
        <key>NSPhotoLibraryAddUsageDescription</key>
        <string></string>
    

    Here is my actual code, on button press I am checking if permission is there to open gallery

    TextButton.icon(
        icon: const Icon(
           Icons.photo_library_rounded,
              size: 50,
            ),
       label: const Text('Pick from gallery',
          style: TextStyle(fontSize: 20)),
        onPressed: () {
          PermissionService().openPhotoGallery();
         },
    ),
    

    Here is my permissions class. The app doesn't open the app settings instead it just loads up the settings on ios.

    import 'package:permission_handler/permission_handler.dart';
    
    class PermissionService {
    
      openPhotoGallery() async {
        var galleryAccessStatus = await Permission.mediaLibrary.status;
        if(!galleryAccessStatus.isGranted) {
          var status = await Permission.mediaLibrary.request();
          if(status.isPermanentlyDenied) {
            await openAppSettings();
          }
        }
      }
    }
    

    Adding this snip after viewing Zeeyans answer. The openAppSettings does not open the app settings page instead it opens the Settings page. enter image description here

  • Sumchans
    Sumchans over 2 years
    The issue is the openAppSettings() does not open the app settings instead it opens the ios setting page. I have added more context to the original post.