How to check Internet connection on a Cordova App?
Asked Answered
H

6

16

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!".

Hexapody answered 2/8, 2016 at 12:30 Comment(1)
and what seems to be the problem? can't you do if you get an error when trying to connect to service show that message?Fitzger
P
17

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.

Paquin answered 2/8, 2016 at 12:33 Comment(0)
B
10

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);
Baker answered 2/8, 2016 at 12:47 Comment(0)
N
7

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 .... )

Newark answered 10/12, 2019 at 16:11 Comment(0)
D
4

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();
Dysart answered 2/8, 2016 at 12:38 Comment(1)
when the device connected via WIFI, this function will give us a true even if there is no internet connection in the hotspotDeclare
S
0

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();
}
Scourge answered 24/3, 2021 at 11:55 Comment(0)
S
0

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

Savory answered 28/7, 2024 at 14:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.