user16829612
user16829612

Reputation:

Flutter permission_handler : request after denied two times

I use permission_handler package to get location permission. I use the following code and this is working fine. but if denied two times request dialog isn't display again. how can i request permission while user already denied it?

_checkPermission() async{
    if(await PermissionHandler.Permission.location.request().isGranted){
      _run();
    }else{
      _checkPermission();
    }
  }

Upvotes: 0

Views: 4906

Answers (2)

sohrabonline
sohrabonline

Reputation: 664

Add this to pod file (in Android Studio: ios -> podfile ) and you can change for your needs (permissions):

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.delete 'IPHONEOS_DEPLOYMENT_TARGET'
      # You can remove unused permissions here
      # for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
      # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
      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
end

Upvotes: 1

user13296944
user13296944

Reputation:

To avoid spam, you can't ask more than two time for an access on a device, but you can open the app setting with the openAppSettings() function.

See this code :

Future<void> _selectContact() async {

  // stocke in status var the result of request
  PermissionStatus status = await Permission.contacts.request();


  if (status == PermissionStatus.denied) {
    // if the user deny, so we cancel the function

    return;
  } else if (status == PermissionStatus.permanentlyDenied) {
    // if the user permanently deny (it's the case if user deny two times)
    
    // we display a popup for say "Hey, you absolutely need this access for use this fonctionnnality, do you want allow it in parameters ?"

    showDialog<String>(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        title: const Text('Allow app to acess your contacts list ?'),
        content: const Text(
            'You need to allow contact access in parameters for use your contacts list in the app'),
        actions: <Widget>[

          // if user deny again, we do nothing
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Don\'t allow'),
          ),

          // if user is agree, you can redirect him to the app parameters :)
          TextButton(
            onPressed: () {
              openAppSettings();
              Navigator.pop(context);
            },
            child: const Text('Allow'),
          ),

        ],
      ),
    );
    return;
  }

  // code to execute if access is granded
}

The last thing you need to know is that, don't use PermissionHandler.Permission.location.request().isGranted but PermissionHandler.Permission.location.request().

If the user deny so PermissionHandler.Permission.location.request().isGranted will return false but if you use PermissionHandler.Permission.location.request(), you can check if it's just a deny or a permanently deny ;)

last things await Permission.contacts.isDenied; and await Permission.contacts.isPermanentlyDenied; can't be use in this case because .isPermanentlyDenied return true only if the user select "permenently deny". If the user just deny two time so .isPermanentlyDenied will return false.

Upvotes: 2

Related Questions