Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen to inAppBrowser closed event

The following is the code that I am currently working with. I would like to understand how do I listen to the inAppBrowser close event? When some closes the inAppBrowser, the app should show some kind of alert message.

According to the documentation I have to use browser.close(), but this doesn't work.

import { Component } from '@angular/core';
import { NavController, NavParams, Platform, LoadingController } from 'ionic-angular';
import { InAppBrowser } from 'ionic-native';

@Component({
  selector: 'page-payment-information',
  templateUrl: 'payment-information.html'
})
export class PaymentInformationPage {

  constructor( public navCtrl: NavController, public navParams: NavParams, public platform: Platform, public loadingCtrl: LoadingController ) {
    this.platform = platform;
  }

  paymentForm(){
      let browser = new InAppBrowser('https://www.stackoverflow.com', '_blank', 'hidden=no,location=no,clearsessioncache=yes,clearcache=yes&enableViewportScale=yes');
      browser.close();
  }

}
like image 398
Red Virus Avatar asked Feb 19 '17 00:02

Red Virus


2 Answers

There are few things that is wrong in your code.

From the documentation browser.hide() doesn't do what you want to do.

Hides an InAppBrowser window that is currently shown. Calling this has no effect if the InAppBrowser was already hidden.

_blank replace it with _system because _blank opens in the inAppBrowser. You won't be able to listen for close event.

Now, you can subscribe to the browser and listen for it's events e.g.

//Events: loadstart, loadstop, loaderror, exit
browser.on('exit').subscribe(() => {
    console.log('browser closed');
}, err => {
    console.error(err);
});
like image 191
Navneil Naicker Avatar answered Nov 14 '22 23:11

Navneil Naicker


I know it to late this issue was 3 years ago but the issue is an issue so here is the solution

1st import InAppBrowser

import {InAppBrowser} from '@ionic-native/in-app-browser/ngx';

2nd add to constructor()

constructor(private iab: InAppBrowser ) {}

3rd Now you can use on('xxx') where xxx is the event you want Events list: 'loadstart' | 'loadstop' | 'loaderror' | 'exit' | 'beforeload' | 'message' | 'customscheme'

In your case, you want to listen when inAppBrowser is closed check follwoing example:

anyFunName(url){
let openBrowser = this.iab.create(url, '_blank');
openBrowser.on('exit').subscribe(event => {
  console.log("inAppBrowser is closed now");
  // your action here when close inAppBrowser
});
}
like image 21
Eng.Haytham Badran Avatar answered Nov 15 '22 01:11

Eng.Haytham Badran