cordova-plugin-network-information icon indicating copy to clipboard operation
cordova-plugin-network-information copied to clipboard

Online event not firing on iOS or Android

Open JoeOsterfeld opened this issue 6 years ago • 3 comments

Steps to repeat:

  1. Create a Cordova/Ionic app.
  2. Listen for onConnect events:
document.addEventListener('online', onOnline, false);
function onOnline() {
    // This never fires
    console.log('I\'m online!');
}
Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : android 7.1.2, ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 2.1.4, (and 14 other plugins)

System:
   Android SDK Tools : 26.1.1 (/Users/username/Library/Android/sdk)
   ios-deploy        : 1.9.4
   ios-sim           : 7.0.0
   NodeJS            : v10.13.0 (/Users/username/.nvm/versions/node/v10.13.0/bin/node)
   npm               : 6.4.1
   OS                : macOS High Sierra
   Xcode             : Xcode 10.1 Build version 10B61

JoeOsterfeld avatar Dec 04 '18 14:12 JoeOsterfeld

Hey, I've tested it on Android.

If you run an Ionic app, the 'online' event is normally added after the deviceready (platform.ready()) event. The 'online' or 'offline' event is already fired then. (The first events are fired even before the first deviceready event on android);

You could always read the type of navigator.connection when you need to:

const networkState = navigator.connection.type;

if (networkState === 'none') {
   console.log('We are offline');
} else {
   console.log('We are online');
}

In our company, we allways check the navigator.connection.type before every api call.

If you need more help attach your project.

Kind regards, Pieter

PieterVanPoyer avatar Dec 22 '18 22:12 PieterVanPoyer

the solution that it poses @PieterVanPoyer has solved the problem for me pm android, but in them there is iOS no solution, at the moment I am satisfied.

anaaChacon avatar Jan 14 '19 18:01 anaaChacon

Any update on this issue?

TeChn4K avatar May 13 '19 15:05 TeChn4K