Search code examples
ionic-frameworkcapacitor

Ionic File Opener - Detect when file is closed


I'm using Ionic and Capacitor to download a PDF and then open it in the default PDF reader of my smartphone. My method looks like that :

import { Directory, Filesystem } from "@capacitor/filesystem";
import { FileOpener } from '@ionic-native/file-opener';

getReceiptPDF({ pathParams: { receiptID: props.receipt.receiptID } }).then((response) => {
    if (response.status == 200) {

        // Custom method using Filesystem to download the file in the generic Documents folder of the smartphone)
        downloadFileMobile(response, props.receipt.receiptID + ".pdf"); 

        Filesystem.getUri({
            directory: Directory.Documents,
            path: props.receipt.receiptID + ".pdf"
        }).then((getUriResult) => {
            const path = getUriResult.uri;
            console.debug("PATH : " + path);
            FileOpener.open(path, 'application/pdf')
            .catch((error) => {
                console.debug(error);
            })
        });
    }
})

It works well, but now I wanna trigger a method when my file is closed, but I don't see any callback for that in the File Opener documentation.

What can I do to handle that?


Solution

  • You can use the Capacitor App plugin for this, e.g.:

    import { App } from '@capacitor/app';
    
    const waitForResume = () => {
      return new Promise((resolve) => {
        App.addListener('appStateChange', async (state) => {
          if (state.isActive) {
            resolve();
          }
        });
      });
    };