Im confused when i want to make a ionic network plugin to check for connection all the time when user uses an app. I’ve tried an example from ionic website but without any results.
import { Network } from '@ionic-native/network/ngx'; //ngx
constructor(private network: Network) { }
...
// watch network for a disconnect
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();
Anyone say that i need to add it in app.component.ts but still doesnt work. Somebody can help ? THanks
Yes it gives “true” while network is connected but did not detecting when network is gone ?
how can I do that… ?
in this case I need to import it all pages of the app, is it possible that I can put the app.component.ts and it is available on whole project ?
I’ve tested on Chrome @ iMac and it really fails detecting offline …
I also removed internet cable to be sure there was no internet.
Nothing triggered.
On Devices it works well.
Have searched some info and only i got was this:
And yes to have in all App, do it in App.component
No need to add login page?. I still can’t achieve it even i added it on app.component.ts.
First, I tested on app.component.ts and run it on emulator. it didn’t work.
And I tried to add those code on login page as well. it didn’t show the message too. Sad.
Hello Bro, I have another problem with photo selection and deletion before i upload. Here is my issue . I was finding it and still stuck on it. Could you please help me? Thank you so much
Hi all guys, don’t use plugins for network checking, I got an very very and very easy way to check if the user has internet connection or not, so all we have to do is that to use this:
if (navigator.onLine) {
console.log('Internet is connected');
} else {
console.log('No internet connection');
}
at here navigator is not a variable created by me. The navigator.onLine property provides a boolean value whether or not the user is connected to the internet. … Browser and real device also supports online and offline events whenever the browser is goes offline or comes online .
this is the very easy and working way, I’m sure it will help.
navigator.onLine works really well on browsers but isn’t that much of a use when on mobile devices. It always remains true and does not change to false even if the device is disconnected from the network.
Hello, you said that your code, works perfectly, you tried to disconnect the cable that provide connection to the wifi, in this model I realized that the answer remains true