Ionic 3, how to check network connectivity in each page change

ionic-framework, ionic3

I am searching for a better solution to popup dialog with connection fail, if internet is off at any position of my app.

I have provider to check the connection. But I am not getting good solution to call on each event or on each page change.

Best Solution

you can do it subscribing to an event.

I'll suggest you to do the following;

AppComponent.ts

export class MyApp {  constructor (    private network: Network,    private platform: Platform,    private alertCtrl: AlertController,  ) {    platform.ready().then(() => {      this.listenConnection();    })  }  private listenConnection(): void {    this.network.onDisconnect()      .subscribe(() => {        this.showAlert();      });  }  private showAlert(): void {    // omitted;  }}

This way, you're going to listen the disconnect event in all application.Of course, you can isolate this code in a Provider, and call after the user logged in or some other business rule.

I hope it may help you.