Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ben4
Beginner
92 Views

Check network before open sub page

Jump to solution

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

Tags (2)
0 Kudos
1 Solution
Hamilton_Tenório_da_
Valued Contributor I
92 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

7 Replies
Hamilton_Tenório_da_
Valued Contributor I
93 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

ben4
Beginner
92 Views

Thank you Hamilton!,

 

Let me try now and see how it works..

 

 

Ben

ben4
Beginner
92 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.

ben4
Beginner
92 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();
ben4
Beginner
92 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

Hamilton_Tenório_da_
Valued Contributor I
92 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
    }

 

 

ben4
Beginner
92 Views

Thanks:-)

 

 

Reply