Software Archive
Read-only legacy content
17061 Discussions

Check network before open sub page

ben4
Beginner
729 Views

Hello,

 

I would like to know if there is a way to warn the user that their device should be connected to the internet before loading a sub page please.

Thank you.

 

Ben

0 Kudos
1 Solution
Hamilton_Tenório_da_
Valued Contributor I
729 Views

Ben,

You can use Network Information plugin to do this.

Using this, you can check any time calling a function like this:

function checkConnection() {
    var estadoInternet = navigator.connection.type;
    if (estadoInternet == "none" || estadoInternet == "unknown") {
        navigator.notification.beep(2);
        myApp.showPreloader("Para executar esta função é preciso ter conexão com a internet.");
        setTimeout(function () {
            myApp.hidePreloader();
        }, 2000);
        return false;
    } else {
        return true;
    }
}

 

Hamilton

View solution in original post

0 Kudos
7 Replies
Hamilton_Tenório_da_
Valued Contributor I
730 Views

Ben,

You can use Network Information plugin to do this.

Using this, you can check any time calling a function like this:

function checkConnection() {
    var estadoInternet = navigator.connection.type;
    if (estadoInternet == "none" || estadoInternet == "unknown") {
        navigator.notification.beep(2);
        myApp.showPreloader("Para executar esta função é preciso ter conexão com a internet.");
        setTimeout(function () {
            myApp.hidePreloader();
        }, 2000);
        return false;
    } else {
        return true;
    }
}

 

Hamilton

0 Kudos
ben4
Beginner
729 Views

Thank you Hamilton!,

 

Let me try now and see how it works..

 

 

Ben

0 Kudos
ben4
Beginner
729 Views

I downloaded the plugin and tried to add your code at the bottom on my index.html and stopped both my Wifi and 3G, no warnings are showing.

0 Kudos
ben4
Beginner
729 Views

ok I am going to try with this new code, awaiting for a build to generate...

 

But it seems that after the 2.30 version the API code has changed to this:

 

function checkConnection() {
    var networkState = navigator.connection.type;
 
    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';
 
    alert('Connection type: ' + states[networkState]);
}
 
checkConnection();
0 Kudos
ben4
Beginner
729 Views

ok here is the right integration steps for it to work and it works very well!

 

https://cordova.apache.org/docs/en/2.8.0/cordova/connection/connection.type.html

0 Kudos
Hamilton_Tenório_da_
Valued Contributor I
729 Views

Ben,

I adapted the code to know only if there is connection. That´s why I check only none or unkown.

To work, I put my code inside this:

if (checkConnection()) {
        // do something
    }

 

 

0 Kudos
ben4
Beginner
729 Views

Thanks:-)

 

 

0 Kudos
Reply