Mouhamad Ounayssi
Mouhamad Ounayssi

Reputation: 361

Phonegap Build cordova how to detect no internet connection even wifi connected

Using Phonegap Build for iOS application, How to detect if there is no internet connection on the device even a wifi/wireless connection exist and the device itself is connection to wifi connection but no response from the internet.

There is an activity in the application, needs to connect to the internet to get an online image, so the device is connection to wireless connection but no internet response, we want to check that instead of the loader indication icon still appeared!

Upvotes: 1

Views: 1319

Answers (1)

QuickFix
QuickFix

Reputation: 11721

You can use google's blank page : http://www.google.fr/blank.html

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 if you need to specify a timeout shorter than the default one :

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: 2

Related Questions