Search code examples
iosflutterdartios-permissions

When ask Image getting iOS permission once user didn't allowed the permission how to ask again to allow the permission


once user want to change the profile , but user didn't allowed the permission once user didn't allowed the permission , how to ask every time the permission when user want change the profile (only when user didn't allowed the permission) Flutter on Ios

user must allow the permission


Solution

  • Try below code with a package permission_handler to manage permissions.

        @override
        void initState() {
          super.initState();
          checkPermission();
        }
        
        Future<void> getImage() async {
          PermissionStatus status = await Permission.photos.status;
          if (status.isGranted) {
            // Proceed to pick an image
          } else {
            await requestPermission();
          }
        }
    

    Check the current status of the permission with below method.

        Future<void> checkPermission() async {
          PermissionStatus status = await Permission.photos.status;
        
          if (status.isDenied) {
            // Permission is denied, show a dialog or message to the user
          } else if (status.isGranted) {
            // Permission is granted, proceed with accessing images
          }
        }
    

    If permission is denied,show a dialog explaining why the permission is necessary and how to enable it.

        void showPermissionDialog(BuildContext context) {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Permission Required'),
                content: Text('This app needs permission to access your photos. Please enable it in settings.'),
                actions: <Widget>[
                  TextButton(
                    child: Text('Open Settings'),
                    onPressed: () {
                      openAppSettings();
                    },
                  ),
                  TextButton(
                    child: Text('Cancel'),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ],
              );
            },
          );
    

    If user initially denies the permission but has not permanently denied it, you can request the permission again with below method.

        Future<void> requestPermission() async {
          PermissionStatus status = await Permission.photos.request();
        
          if (status.isGranted) {
            // Permission granted, proceed with accessing images
          } else if (status.isDenied) {
            // Permission still denied, show a dialog
            showPermissionDialog(context);
          } else if (status.isPermanentlyDenied) {
            // Permission is permanently denied
            showPermissionDialog(context);
          }
        }