How can i check the connection off/on using ionic

with what i can replace " Network.connection"? it’s no more valid !! please i need help

import { Network } from '@ionic-native/network';

 isOnline(): boolean {
     if(this.onDevice && Network){
     return Network.connection!== Connection.NONE;
    } else {
      return navigator.onLine; 
  }
 }

As show in the documentation. You can subscribe to

onchange();

could you write an example please ! ?


        this.network.onchange().subscribe((data)=>{
            console.log(data);
        });

Would be the simplest way, but unfortunately I dont think the function works properly, onConnect, onDisconnect and onchange are not triggering?

There were some old similar problems on github like this.

Can someone else try it?

1 Like

Apologies if this is too silly, but you will see that exact behavior if you install the native shim but forget to actually install the underlying cordova plugin.

2 Likes

Not silly at all, Im pretty sure I installed the plugin, Ill check it when I get to the office. But when I try to get the property this.network.type it returns the type of connection properly. It just doesnt give the on change event.

UPDATE:

Checked, and you were indeed right! It seems that I forgot to include the network plugin. Would have never guessed it. As per usual, thanks alot @rapropos!

2 Likes

You can check the following
this.network.onDisconnect().subscribe(() => {
this.status = ‘network was disconnected :-(’;
this.presentToast(‘network was disconnected!’);
});
this.network.onConnect().subscribe(() => {
this.status = ‘network was Connectedd :-(’;
this.presentToast(‘network was Connectedd!’);

  });
  if(this.network.type !== 'none'){
    return true;
  }else if(this.network.type === 'none'){
    this.presentToast('Please Check your network and try again');
  }else{
    this.presentToast('Please Check your network and try again');
  }

Here you check Internet connection 100% working on all devices.

Try in simple way :

  document.addEventListener("online", ()=>{
    console.log("Internet available"); 
  }, false);

  document.addEventListener("offline", ()=>{
    console.log("No internet available"); 
  }, false);