@rodrigojrmartinez wrote:
Hi Guys
I’m developing an ionic 4 app where I need to keep track of the current network status and notify the user whenever the status changes. The app should be supported on browser besides mobile devices, so after checking a while I’ve learned that when on mobile, the Network cordova plugin in should be used. This works just excellent! My problem starts when having to detect this at browser level.
Basically, what I do is to wait for the platform to be ready, check where I’m at, and based on this listen for the proper events besides checking the status at that point, something like.
constructor(platform: Platform, network: Network) { this.platform.ready().then(() => { if (this.platform.is('cordova')) { // this is ok this.listenForDeviceNetwork(); } else { this.listenForBrowserNetwork(); } }); } private listenForBrowserNetwork(){ // also tried with document! window.addEventListener('offline', () => { console.log('now you are offline!'); }); window.addEventListener('online', () => { console.log('now you are online!'); }); this.isOnline = navigator.onLine; }
The problem I’m facing is that the navigator.onLine is always returning true, and the events are never fired. Tryied taking out the internet connection as well as setting to the airplane mode, but noting… Also tried to log the navigator.onLine with an interval and check its value, but it is always returning true
setInterval(() => { console.log('now ', navigator.onLine); }, 200);
This anyone stumbled across this problem? Thanks for the time!
Posts: 1
Participants: 1