ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
503 stars 575 forks source link

App does not wait for user permission to READ and WRITE. #315

Closed jocafi closed 3 years ago

jocafi commented 3 years ago

Bug Report

Plugin(s)

 Filesystem 

Capacitor Version

  @capacitor/ios: not installed
  @capacitor/cli: 3.0.0-rc.0
  @capacitor/core: 3.0.0-rc.0
  @capacitor/android: 3.0.0-rc.0

Platform(s)

Samsung S20, Android 11.

Current Behavior

The app does not wait for the user input to allow or not the permission to READ and WRITE.

Expected Behavior

The app waits for the user input to allow or not the permission to READ and WRITE.

the app does not wait for the user allow or not the permission.

Code Reproduction

Filesystem.requestPermissions().then(() => log.info('Directory permissions: PASSED'),
      error => {
        log.error('Directory permissions: FAILED. Error: ', error);
        this.route.navigateByUrl('/error-page');
      }).catch(error => {
      log.error('Directory permissions: FAILED. Error: ', error);
      this.route.navigateByUrl('/error-page');
    });

Other Technical Details

I am trying to use the Filesystem from Capacitor v. 3.0.0-rc.0, but the app fails to start, because I need to load a config file at the beginning and the app does not wait user input to allow or not the permission to READ and WRITE.

I am using a Samsung S20, Android 11. I added android:requestLegacyExternalStorage="true" to my tag in the AndroidManifest.xml file as described in the documentation.

I tried to use the plugin for android-permissions but I get an error:

NullInjectorError: No provider for AndroidPermissions!

In order to reproduce the error I had to uninstall and install the app again.

Additional Context

jcesarmobile commented 3 years ago

Please, provide a sample app where the problem can be reproduced.

jocafi commented 3 years ago

Here is the sample: https://github.com/jocafi/ionic5-test

jcesarmobile commented 3 years ago

The problem is you are trying to read the folder outside the promise result, so the read code is executed while the prompt is present and makes it return an error. It's a know issue https://github.com/ionic-team/capacitor-plugins/issues/320

But the workaround is simple, wait for the permissions result before trying to call other filesystem methods.

jocafi commented 3 years ago

Move the code that "read the folder" inside the promise for Filesystem.requestPermissions(). The permission dialog will never be shown.

jcesarmobile commented 3 years ago

requestPermission resolves once the user accept or deny the prompt, so the code inside the requestPermission .then is executed. The permission dialog is shown by requestPermission, the problem in your app is the read folder code triggers another prompt, but as one is already shown it gets canceled, and by the issue I linked, the first promise gets rejected instead of the second one.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.