Skip to content Skip to sidebar Skip to footer

Cordova - Checking WIFI Connection To Internet

I use Cordova to develop a smartphone application. In this app, I need to check the internet connection before send request to a server. In order to do that I use the Cordova Conne

Solution 1:

Send an Dummy ajax request before you send the actual request, If you get and Error Code as '0' it means there is no internet connectivity.

$.ajax({
    url: 'TestUrl',
    type: 'GET',
    success: function (data) {
                // Go ahead with you request
    },
    error: function (x, y, z) {
        if (x.status == 0) {
            alert("Please connect to the internet");
        }
       else{
           alert("Other Error Occured")
        }
    }
});

Secondly you can also make you of HTML 5 navigator

var condition = navigator.onLine ? "ONLINE" : "OFFLINE";

Solution 2:

I haven't used cordova much, but I think it cannot recognise if you have internet access, it just recognises if you are connected to a network.

You can do a work-around: try to send your request and catch the error you get when it fails, from there you can tell the user that he doesn't have internet access.


Solution 3:

You can use cordova plugin add org.apache.cordova.network-information to add the Connection feature. These are the Connection docs.

That plugin will give access to window.Connection.

Then you can do something like:

if (navigator.connection.type == Connection.NONE) {
    alert("you're going to need more internet, brah");
}

You'll also get access to other states, such as CELL_2G,CELL_3G, WIFI, etc...


Post a Comment for "Cordova - Checking WIFI Connection To Internet"