Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 check Internet connection

I have installed the Cordova network plugin, but cannot seem to check network connection.

This is my code

import { Network } from 'ionic-native';

declare var navigator: any;
declare var Connection: any; 

this.platform.ready().then(() => {
   if(Network.connection === 'none') {
       let alert = this.alertCtrl.create({
       title: "Internet Connection",
       subTitle:"Please Check Your Network connection",
       buttons: [{
          text: 'Ok',
          handler: () => {
              this.platform.exitApp();
             }
          }]
        });
      alert.present();
     }
  });
}

I get warning message

Native: deviceready did not fire within 2000ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.

How could i go about resolving the issue?

like image 648
Ani Vaishu Avatar asked Nov 25 '25 12:11

Ani Vaishu


2 Answers

Add this under platform ready. Once your app is up and running, try to disconnect and reconnect from\to internet. You should get the messages on your console.

let disconnectSub = Network.onDisconnect().subscribe(() => {
  console.log('you are offline');
});

let connectSub = Network.onConnect().subscribe(()=> {
  console.log('you are online');
});
like image 105
Deepak Avatar answered Nov 28 '25 03:11

Deepak


I made use of the network import and ionic events to manage network disconnection and conection

 checkDisconnection() {
     const disconnectSubscription = this.network.onDisconnect().subscribe(() => {
     this.checkConnection();
     disconnectSubscription.unsubscribe();
     this.nav.push('ErrorsPage');
   });
 }
 checkConnection() {
  const connectSubscription = this.network.onConnect().subscribe(() => {
  this.checkDisconnection();
  connectSubscription.unsubscribe();
  this.nav.pop();
  });
 }

Works perfectly fine. Any more information you need please mention in comments

like image 38
athulpraj Avatar answered Nov 28 '25 02:11

athulpraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!