Tuesday, January 09, 2018

Solution: InAppBrowser opens PDF on Android device

Basic idea: use Android PDF viewer (default is chrome) to open the PDF file.

trick 1: target is '_system' to call PDF viewer

trick 2: pre_url to ensure refresh current page in IAB

Example code:



      let open_window = function(iab, url, target, options){

        let browser = iab.create(url, target, options);
        let pre_url = url;

        browser.on("loadstart")
         .subscribe(
            (res) => {
              if(res.url.indexOf('pdf') > -1) {
                iab.create(res.url, '_system');

                open_window(iab, pre_url, target, options);
              }
              pre_url = res.url;
            },
            err => {
              console.log("InAppBrowser exit Event Error: " + err);
        });


        browser.on("loadSuccess")
         .subscribe(
            (res) => {
              splashScreen.hide();
            },
            err => {
              console.log("InAppBrowser exit Event Error: " + err);
        });

        browser.on("exit")
         .subscribe(
            (res) => {
              platform.exitApp();
              console.log("InAppBrowser exit successfully");
            },
            err => {
              console.log("InAppBrowser exit Event Error: " + err);
        });
      }

      open_window(this.iab, url, target, options);

No comments: