Search code examples
javascriptcordovacordova-plugins

Detect initial network status in a hybrid application


I am implementing cordova-plugin-network-informationand detecting the status connection works correctly, but I am not getting the initial network status, only works when the connection change.

Any improvement or clue to get the real status when the app is initialize?


Solution

  • I solved this problem like this:

    document.addEventListener("deviceready", () => {
            if (navigator.connection.type === Connection.NONE) {
                variableContainingYourNetStatus = false;
            } else {
                variableContainingYourNetStatus = true;
            }
        });
    

    Note the TypeScript-syntax. In JS you might want to change the lambda into a function.

    Basically when deviceready is fired, the plugins are initialized as well. So this is the right time to check your network status and set the variable that contains the status.