2017-04-02 69 views
3

如果有網絡連接但互聯網不工作。例如有一個WiFi連接,但WiFi沒有互聯網連接,瀏覽器觀察者應該觸發「loaderror」事件。ionic2 InAppBrowser(離子本機)loaderror不起作用

代碼:

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(); 
    }); 
} 
} 

但它總是加載谷歌網頁,甚至都能得到加載clearsessioncache和網絡斷開後。我在android和桌面上測試了這個。

+0

當url有問題時發生了什麼? – Sampath

+0

什麼都不顯示白頁,只在那裏。 –

+0

我改變了加載錯誤的代碼我的火腳本,它得到加載,但10秒後。當我啓動我的應用程序需要10秒,當loaderror發生時,它再次需要10秒,所以共20。 –

回答

2

您可以嘗試如下所示。

注意:因此它有一定的延遲,你必須使用loader如下所示。

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') }); 
    } 
+0

'if(this.network.type!=='none'){ \t \t \t \t const loading = this.loadingCtrl.create(); \t \t \t \t loading.present(); \t \t \t \t const browser = this.iab.create('http://www.google.com','_blank','location=no,clearsessioncache = yes'); \t \t \t \t \t \t const的子= browser.on( 'loadstart')訂閱(()=> { \t \t \t \t \t loading.dismiss(); \t \t \t \t \t sub.unsubscribe() ; \t \t \t \t}, \t \t \t \t \t ERR => {的console.log( '錯誤'); }, \t \t \t \t \t()=> {console.log('success')}); \t \t \t}'**即使在加載頁面之後,它並沒有在控制檯上顯示SUCCESS消息,並且出現錯誤。** –

+0

完全使用我給你的代碼片段。它完全適合我。 – Sampath

+0

它正在工作,但它需要太多的時間,因爲我上面提到。 –