Home > OS >  Ask user permission on ios with flutter - flutter
Ask user permission on ios with flutter - flutter

Time:10-08

my app let users to change their profile picture using the camera or gallery, the picture is uploaded to Firebase storage and then the image link is updated in the user's Firestore information,

in android devices the dialog that asks for permission appears and its working perfectly, but in ios no dialog shown and camera or gallery doesn't open at all, here is the code:

uploadImageCamera() async {
    final _firebaseStorage = FirebaseStorage.instance;
    final _userStorage =
        FirebaseFirestore.instance.collection("Users").doc(user!.uid);
    final _imagePicker = ImagePicker();
    PickedFile? image;
    //Check Permissions
    await Permission.camera.request();

    var permissionStatus = await Permission.camera.status;

    if (permissionStatus.isGranted) {
      //Select Image
      image = await _imagePicker.getImage(source: ImageSource.camera);
      var file = File(image!.path);

      if (image != null) {
        //Upload to Firebase
        var snapshot = await _firebaseStorage
            .ref()
            .child('users_images/${loggedInUser.name}.jpg')
            .putFile(file);
        var downloadUrl = await snapshot.ref.getDownloadURL();
        setState(() {
          imageUrl = downloadUrl;
          loggedInUser.profileImage = imageUrl;
          _userStorage.update({'profileImage': imageUrl});
        });
      } else {
        print('No Image Path Received');
      }
    } else {
      print("Nothing....!");
    }
  }

i have added the camera and gallery permissions to the info.plist file:

<key>NSPhotoLibraryUsageDescription</key>
<string>App needs access to photo lib for profile images</string>

<key>NSCameraUsageDescription</key>
<string>To capture profile photo please grant camera access</string>

now when i test on ios device dialog doesn't open to require access permission, so am not sure what i am missing, i will appreciate any help. Thanks

CodePudding user response:

You can use flutter package like permissionHandler to make you easier to handle permission in flutter.

Example:

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

// You can can also directly ask the permission about its status.
if (await Permission.location.isRestricted) {
  // The OS restricts access, for example because of parental controls.
}

If you write the code inside void or other function, dont forget to add async

permissionChecker() async {
//your code here
}

CodePudding user response:

In Podfile.lock

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

Replace with below

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=1',

               ## 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.appTrackingTransparency
                ##'PERMISSION_APP_TRACKING_TRANSPARENCY=1',

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

               ## dart: PermissionGroup.sensors
               #'PERMISSION_SENSORS=0'
             ]
    end
  end
end
  • Related