miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.33k stars 663 forks source link

The app is moved to inactive state when the file picker is presented on iOS #813

Closed rohitrvayuz closed 3 years ago

rohitrvayuz commented 3 years ago

Describe the bug The image picker or the document picker on iOS opens in new window and moves the app to inactive state and upon selecting the file it moves the app back to the resume state. The problem here is that I have a banking app which asks for PIN every time the app is resumed and due to this I am not able to implement the functionality. The default ImagePicker open the gallery modally on iOS but it does not support pdf files therefore I used your file_picker but it does not open the gallery and document picker modally.

Platform

Platform OS version iOS 14.6

How are you picking?

try {
      FilePickerResult result =
       await FilePicker.platform.pickFiles(type: FileType.image);

       if (result != null) {
         File file = File(result.files.single.path);
         log('$file', name: 'Picker file');
       } else {
         // User canceled the picker
         log('User canceled the picker', name: 'Picker error');
       }
     } catch (e) {
       log('', name: 'Picker error', error: e);
    }

Flutter Version details

[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.2 20G95 darwin-arm, locale en-IN) • Flutter version 2.2.3 at /Users/rohitrajput/flutter • Framework revision f4abaa0735 (9 weeks ago), 2021-07-01 12:46:11 -0700 • Engine revision 241c87ad80 • Dart version 2.13.4

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/rohitrajput/Library/Android/sdk • Platform android-31, build-tools 31.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.5.1, Build version 12E507 • CocoaPods version 1.10.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.59.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.25.0

[✓] Connected device (2 available) • iPhone 12 Pro Max (mobile) • E52232D1-52A3-479D-A247-DC2798F51CAB • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator) • Chrome (web) • chrome • web-javascript • Google Chrome 92.0.4515.159

• No issues found! @miguelpruivo @jamesdixon @pchampio

miguelpruivo commented 3 years ago

Hum. The file_picker is also opened in a modal and the app is kept on background while doing the task of picking. Are you telling me that the image_picker doesn’t do that?

rohitrvayuz commented 3 years ago

Hum. The file_picker is also opened in a modal and the app is kept on background while doing the task of picking. Are you telling me that the image_picker doesn’t do that?

@miguelpruivo Currently, both document picker and image/gallery picker on iOS are not being opened modally. They open on full screen and put the app in inactive state.

This is the log when I open the picker, select a file and then the picker closes

[log] app is inactive [log] app is paused [log] app is inactive [App Lifecycle] app is resumed 2 [Lifecycle] AppLifecycleState.resumed [Picker result] [/Users/username/Library/Developer/CoreSimulator/Devices/E52232D1-52A3-479D-A247-DC2798F51CAB/data/Containers/Data/Application/4190E9EB-7CB2-4CE0-B295-1CC6E5393215/tmp/com.app-Inbox/sample.pdf] [Picker file] File: '/Users/username/Library/Developer/CoreSimulator/Devices/E52232D1-52A3-479D-A247-DC2798F51CAB/data/Containers/Data/Application/4190E9EB-7CB2-4CE0-B295-1CC6E5393215/tmp/com.app-Inbox/sample.pdf'

@miguelpruivo The image_picker opens the view modally without putting the app in background.

miguelpruivo commented 3 years ago

Ok, I'll take a look.

rohitrvayuz commented 3 years ago

Ok, I'll take a look.

@miguelpruivo Thanks. Or if you could just guide me or tell me a hint where to look at then I can fork the repo and submit the pull request. I have never created packages for flutter before, that's why I am asking

miguelpruivo commented 3 years ago

@rohitrvayuz I'm right now looking into it, however, what is the plugin that you're saying that opens modally? Because the image_picker opens it exactly the same way as this one (using UIModalPresentationCurrentContext presentation).

Nevertheless, I can switch it for a modal.

rohitrvayuz commented 3 years ago

@rohitrvayuz I'm right now looking into it, however, what is the plugin that you're saying that opens modally? Because the image_picker opens it exactly the same way as this one (using UIModalPresentationCurrentContext presentation).

Nevertheless, I can switch it for a modal.

flutter_document_picker 4.0.1 and image_picker 0.8.3+3

miguelpruivo commented 3 years ago

You can see here that it's using the UIModalPresentationCurrentContext which displays the picker in a separate ViewController, hence, making the app going into background mode — exactly the same behaviour of the file_picker.

miguelpruivo commented 3 years ago

I can update it to the desired behaviour, I'm just wondering how can it be since they are both using the same way of presentation when one is clearly using UIModalPresentationFormSheet.

The document_picker is using the automatic mode which can result in the modal sheet, yes.

rohitrvayuz commented 3 years ago

I can update it to the desired behaviour, I'm just wondering how can it be since they are both using the same way of presentation when one is clearly using UIModalPresentationFormSheet.

@miguelpruivo Ok. Thanks.

The document_picker is using the automatic mode which can result in the modal sheet, yes.

Yes, this is what I want actually. Check this

miguelpruivo commented 3 years ago

I'm just surprised of image_picker displaying that way too, are you sure? Because by the code it doesn't look like it.

miguelpruivo commented 3 years ago

Done and published with 4.0.1. Let me know if it's good for you now. Thank you!

rohitrvayuz commented 3 years ago

Done and published with 4.0.1. Let me know if it's good for you now. Thank you!

Thanks a lot @miguelpruivo. It is working as expected now.

MsXam commented 3 years ago

Unfortunately this PR will cause the FilePicker to fail and throw an exception if the user just swipes down the modal picker window rather than tapping cancel.

To reproduce :

If you encapsulate your code within a try..catch .. you will see the exception that is raised :

PlatformException: Multiple requests. Cancelled by a second request.

Sample code


    FilePickerResult fileResult;
    // FilePicker.platform.clearTemporaryFiles();
    try {
      if (DeviceHelper.isAndroid) {
        fileResult = await FilePicker.platform.pickFiles(
          type: FileType.any,
        );
      } else {
        fileResult = await FilePicker.platform
            .pickFiles(type: FileType.custom, allowedExtensions: [
          'ttf',
        ]);
      }
    } on Exception catch (e) {
      DialogHelper.alert(
          'File error', 'It was not possible to open the file', context);
      return;
    } 

The only solution if we still want to keep this new way of showing the modal dialog is to always do a :

FilePicker.platform.clearTemporaryFiles();

Prior to showing the dialog but then we loose the benefits of caching.

miguelpruivo commented 3 years ago

@MsXam I might have to reset after dismissing. I’ll take a look.

alexda12 commented 3 years ago

@MsXam I have this also - do you have a fix for this ?

miguelpruivo commented 3 years ago

This has now been fixed with 4.1.1. Thank you all.

Andrewcpu commented 2 years ago

This seems to be the case on Android still, I'm loading files the same way as listed above and I'm on ^4.2.7. Upon calling pickFiles the AppState goes into paused, and upon returning becomes resumes.