Geziel Carvalho
Geziel Carvalho

Reputation: 301

How to check Internet connection on a Cordova App?

I tried some suggestions, such as navigator.onLine, but even in flight mode, my app "thinks" its online.

I found some suggestions with ajax too, but I just want to check if I'm online to open an external web page. If not, I intend to show a message such as "Your device seems to be offline. Check your connection!".

Upvotes: 16

Views: 27242

Answers (6)

Arekhandia Harry
Arekhandia Harry

Reputation: 11

I’ve finally solved this issue. I was building a mobile app with angular with Cordova. It works well on the web but doesn’t work on the android environment.

What I did was just to install the cordova-plugin-network-information and it started working.

I didn’t write any extra logic based on network event and states. The navigator contructor works with the web navigator.onLine

It works both in flight mode and when mobile data is turned off

Upvotes: 0

Haseeb Javed
Haseeb Javed

Reputation: 93

you can check the connectivity of your android device to internet by using cordova-plugin-network-information plugin. furthermore if you want to perform certain task on the availability of the internet, see below:

if (navigator.connection.type == "none") {
    alert("No Internet Connection...");
    
}
else {
    yourdesiredFunction();
}

Upvotes: 0

Ali Mohammad
Ali Mohammad

Reputation: 850

you can use this plugin

then you can use it everywhere in your App without to import it

if(navigator.connection.type === 'none') {
   alert('there is no internet')
 }

and you can add it into setInterval to check for internet connection every 5 seconds

  setInterval(() => {
        if(navigator.connection.type === 'none') {
            alert('there is no internet')
        } }, 5000);

there are another values for "avigator.connection.type" for example when there is internet then the value of "avigator.connection.type" is the type of connection (wifi, 4g, 3g, ethernet on windows .... )

Upvotes: 7

Tuhin
Tuhin

Reputation: 3373

Download the plugin: https://www.npmjs.com/package/cordova-plugin-network-information

And Try This.

document.addEventListener("deviceready", function(e){
        console.log(navigator.connection.type);
        document.addEventListener("offline", function(e){
                            alert("NO_NETWORK");

        }, false);  
}, false);  

offline

The event fires when an application goes offline, and the device is not connected to the Internet.

document.addEventListener("offline", yourCallbackFunction, false);

Upvotes: 10

Vindhya Pratap Singh
Vindhya Pratap Singh

Reputation: 556

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

Upvotes: 4

Gandhi
Gandhi

Reputation: 11935

The best approach is to use cordova network information plugin which does the job for you seamlessly. This plugin provides information about the device's cellular and wifi connection, and whether the device has an internet connection.

You check out the official github page of this plugin for more info on the same. Hope it helps.

Upvotes: 17

Related Questions