Ionic2 InAppBrowser (Ionic native) loaderror not working

If there is a network connection, but if the Internet is not working. For example, there is a Wi-Fi connection, but the Wi-Fi has no internet connection, than the monitored browser should fire a "loaderror" event.

Code:

import { Component } from '@angular/core';
import { Platform,AlertController,LoadingController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Network } from '@ionic-native/network'; 
import { HomePage } from '../pages/home/home';
import { InAppBrowser } from '@ionic-native/in-app-browser';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  public rootPage:any = HomePage;

  constructor(private platform: Platform,private statusBar: StatusBar,private splashScreen: SplashScreen, private network: Network, private alertCtrl: AlertController, private iab: InAppBrowser, private loadingCtrl: LoadingController) {

platform.ready().then(() => {
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.

   let alert = this.alertCtrl.create({
            title: "Network Problem",
            subTitle: 'Internet is not connected' ,
            buttons: [
                {
                    text: 'Close', handler: () => {
                        this.platform.exitApp();
                    }   
                }
            ]
        });
        const loading = this.loadingCtrl.create();
        if(this.network.type !== 'none') {
            loading.present();
            const browser = this.iab.create('http://www.google.com','_blank','location=no,clearsessioncache=yes');
            browser.on("loaderror").subscribe(() => {
                loading.dismiss();
                this.iab.create('http://192.168.2.222/users/dashboard','_blank','location=no,clearsesioncache=yes');
            });
            const sub = browser.on('loadstart').subscribe(() => {
                loading.dismiss();
                sub.unsubscribe();
            },
                err => { loading.dismiss(); console.log('error') ; alert.present()},
                () => { loading.dismiss(); console.log('success') });
        }                   

        /// check connectivity automatically .

        let connectSubscription = network.onConnect().subscribe(() => {
                loading.present();
                const browser = this.iab.create('http://www.google.com','_blank','location=no,clearsesioncache=yes');
                browser.on("loaderror").subscribe(() => {
                    loading.dismiss();
                    this.iab.create('http://192.168.2.222/users/dashboard','_blank','location=no,clearsesioncache=yes');
                }); 
        }); 
        let disconnectSubscription = network.onDisconnect().subscribe(() => {
                const browser = this.iab.create("error.html",'_blank','location=no');       
        }); 

    statusBar.styleDefault();
    splashScreen.hide();
    });
 }
}

      

But it always loads the google page even after clearsessioncache and internet connection is always loaded. I've tested this on both android and desktop.

+3


source to share


1 answer


You can try as shown below.

Note: Hence, you have a certain delay, you must use loader

as shown below.



 goToWebPage() {
    const loading = this.loadingCtrl.create();//use loader
    loading.present();
    const browser = this.iab.create('http://www.google.com');
    const sub = browser.on('loadstart').subscribe(() => {
       loading.dismiss();
       sub.unsubscribe();
    },
      err => {  loading.dismiss();console.log('error'); },
      () => { loading.dismiss();console.log('success') });
  }

      

+2


source







All Articles