pwlin / cordova-plugin-file-opener2

A File Opener Plugin for Cordova
MIT License
314 stars 583 forks source link

The second "open in" menu is not shown on iOS #305

Closed aleksilahis closed 3 years ago

aleksilahis commented 3 years ago

Expected Behaviour

If showOpenWithDialog is called multiple times, the native "open in" menu is displayed for each files.

Actual Behaviour

The menu is displayed only for the first file. The following error is displayed in console:

2020-08-13 16:08:40.107951+0300 App[14160:747672] Warning: Attempt to present <_UIDICActivityViewController: 0x7faaaa92d800> on <Capacitor.CAPBridgeViewController: 0x7faaa960be90> which is already presenting <_UIDICActivityViewController: 0x7faaa98c7600>

So it doesn't wait until the menu is closed before attempting to open it for the next file(s).

Reproduce Scenario (including but not limited to)

Steps to Reproduce

Call showOpenWithDialog for different files one after the other.

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iPad Pro (iOS 12.2) iPhone SE (2nd gen) simulator (iOS 13.6)

Cordova CLI info

Using this plugin with Capacitor (https://github.com/ionic-team/capacitor).

Versions:

@capacitor/cli: 2.4.0 @capacitor/core: 2.4.0 @capacitor/ios: 2.4.0

Plugin version

cordova plugin version | grep cordova-plugin-file-opener2

Here is the output:

"cordova-plugin-file-opener2": 3.0.4

Sample Code that illustrates the problem

public async showOpenWithDialogs(): Promise<void> {
  const fileWriteResult = await this.saveFileInMobile(`data:application/pdf;base64,' randomData`, ‘File1’, FilesystemEncoding.UTF8);
  const fileWriteResult2 = await this.saveFileInMobile(`data:application/pdf;base64,' randomData`, ‘File2’, FilesystemEncoding.UTF8);

  await this.fileOpener.showOpenWithDialog(fileWriteResult.uri, ‘application/pdf’);
  console.log(‘first file was opened’);

  await this.fileOpener.showOpenWithDialog(fileWriteResult2.uri, ‘application/pdf’);
  console.log(‘second file opened');
}

Logs taken while reproducing problem

[log] - first file was opened 
2020-08-31 13:39:42.477618+0300 App[84907:13446735] Path parameter not encoded. Building file URL encoding it...
2020-08-31 13:39:42.477946+0300 App[84907:13446735] looking for file at file:///Users/aleksi/Library/Developer/CoreSimulator/Devices/ED160F57-3CF7-4337-98CA-B3519E3FC723/data/Containers/Data/Application/78236FAA-28D3-48D3-A456-232E46613E2B/Documents/File2
[log] - second file opened
[log] - Done opening files2020-08-31 13:39:43.440226+0300 App[84907:13446735] Warning: Attempt to present <_UIDICActivityViewController: 0x7fdfdc008c00> on <Capacitor.CAPBridgeViewController: 0x7fdfda609120> which is already presenting <_UIDICActivityViewController: 0x7fdfdc022000>
pwlin commented 3 years ago

Steps to Reproduce Call showOpenWithDialog for different files one after the other.

How does that work? Are you doing

showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...)

Before waiting for the previous calls to be done?

aleksilahis commented 3 years ago

Steps to Reproduce Call showOpenWithDialog for different files one after the other.

How does that work? Are you doing

showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...) showOpenWithDialog(...)

Before waiting for the previous calls to be done?

I'm calling showOpenWithDialog inside observables. Those are executed sequentially, so they should wait for showOpenWithDialog to be resolved. I will double-check my code and clarify reproduction steps once I'm back at the office and can use my Mac for further investigation. Just to mention, on Android the same code works fine and action for each file can be selected one by one.

aleksilahis commented 3 years ago

I have now investigated this a little bit more and added sample code for reproduction.

aleksilahis commented 3 years ago

Found a solution for this. Shortly: I added documentInteractionControllerDidDismissOpenInMenu function to UiDocumentInteractionControllerDelegate and moved the return clause from line 119 into the callback function in this file https://github.com/pwlin/cordova-plugin-file-opener2/blob/master/src/ios/FileOpener2.m. May I create a pull request?

pwlin commented 3 years ago

Hi @aleksilahis thank you very much for your work. I have merged this and pushed a new version 3.0.5 to npm. Can you please test if everything is ok?

aleksilahis commented 3 years ago

@pwlin thank you! The functionality has been verified with a real device and simulators, so closing this issue.