How to check if there is internet connection on app load


#1

How to check if there is internet connection on app load. I tried onDisconnect and onConnect only works if we connect or disconnect while we are in the middle of running the app.

I even tried this -----

 if(!this.network.type){
        let alert = this.alertCtrl.create({
          title: 'No network',
          message: 'Check your internet connection',
          buttons: [{
            text: "OK",
            handler: () => { this.platform.exitApp(); }
          }]
        })
        alert.present();
      }

#2

That’s pretty close to something I have that works:

constructor(private _net: Network) {}
isConnected(): boolean {
  let conntype = this._net.type;
  return conntype && conntype !== 'unknown' && conntype !== 'none';
}

Naturally, you have to make sure the platform is ready before trying this, and one very easy mistake to make here is to install @ionic-native/network but forget to install the actual cordova plugin.


#3

what is isConnected() in your solution


#4

It’s simply a function he’s defined.

You would use it in place of:
if(!this.network.type){

becomes:
if(!this.isConnected())