candlejack
candlejack

Reputation: 1209

What is the best way to check if the device has Internet access?

I want to check if the device has internet access. From the official cordova documentation:

This code, is only to get the connection type

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();

But how can I check if the device really have internet access? That is, because it may be that the device is connected to a router using a WiFi connection type, but that it has no access to internet but only local access. In this case the checkConnection(); will return: WiFi connection.

Or that is connected to a 3G network phone operator, but the user does not have sufficient credits for the internet. In this case the checkConnection(); will return: Cell 3G connection

But in both cases the user have not a real internet access eventhough if connected.

The only thing that I think is to ping google or another server to check if there really has internet access, in the case of states[networkState] != No network connection

Or do you think navigator.onLine can run on all devices (Android, iOS, BlackBery 10?

I hope I have expressed well, my English really sucks.

Upvotes: 2

Views: 5285

Answers (2)

Eagle_one
Eagle_one

Reputation: 426

There is a plugin that helps you to do that: Network information

try it with a test function to an URL that test your request if it fails then you do somthing else, as many answers explained here, or use

document.addEventListener("online", onOnline, false);
function onOnline() {  // Handle the online event}

document.addEventListener("offline", onOffline, false);
function onOffline() {  // Handle the offline event}

Remember to wrap these functions inside a document.addEventListener('deviceready', function() {}, false);

Upvotes: 3

Tushar
Tushar

Reputation: 3052

Hmm... here is a discussion http://iswwwup.com/t/f3e5374b74ca/android-cordova-plugin-to-detect-internet-connection.html

And the solution they mentioned...

A function to test this address to check if the device has access to internet would look like this :

function testInternet(win,fail){
    $.get("http://www.google.fr/blank.html").done(win).fail(fail);
}
Or ,

function testInternet(win,fail){
    $.ajax({
         url:"http://www.google.fr/blank.html",
         timeout:5000,   //timeout to 5s
         type: "GET",
         cache: false
       }).done(win).fail(fail); 
}

Upvotes: 4

Related Questions