Get Network Information using Phonegap / Apache Cordova


how to get network information using phonegap

This articles explains you, how to get network information using phonegap / apache cordova. Here we’re going to use network plugin ( ). It provides information about the device’s cellular and wifi connection, and whether the device has an internet connection.

Step 1:

Create new PhoneGap / apache Cordova project & add platform

Step 2:

Add Network plugin using the following commands

For 5.0+

cordova plugin add cordova-plugin-network-information

For Older Version

cordova plugin add

Example code for Phonegap / Apache Cordova Network Plugin

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]);
<button onclick="checkConnection()">Get Network Info</button>

This is the code for get network information using phonegap / apache cordova. The output of this code will alert your current network type, navigator.connection.type returns network type. you can also use network events for checking the internet connection like online / offline events.

  • Giulia

    I’ve tried to do this but it doesn’t work. I use phonegap and the version of cordova is the 5.0.0. I add the two platforms and after the latest version of this plugin. I put a button into index.html and onClick of it, call a function that verify if the mobile is online or not. I use the same code. The alert doesn’t appear. The code is locked. I’ve tried to change Connection.UNKNOWN into navigator.connection.UNKNOWN (and all of the variables) but if I print states[navigator.networkState], the result is No network connection even if my mobile is connected to a wifi. If I print networkState, the result is Unknown.
    What am I doing wrong?
    Thank you so much

    • Please make sure, you’ve installed network plugin.
      if installed, please check you’ve included “cordova.js” file

      • Giulia

        Yes, i’ve installed it and in the index.html I’ve included cordova.js.

        • Does that shows any errors?

          • Giulia

            No, any errors.
            The function is onclick attribute, sorry.

          • working now?

          • Giulia

            No, I’ve written “onClick” in the comment but in the code it’s always “onclick”.
            So, it doesn’t work yet.

          • Giulia

            I solved. The problem is that with phonegap doesn’t work. It’s necessary do an apk or a ipa.
            Thanks anyway.

        • please check your spelling in onclick it’s not “onClick”

  • Hus

    when I am using mobile data(H+/H mostly), it says unknown connection .. why ?

    • Cordova has the following network types 2G,3G,4G,WiFi, Ethernet, generic connections only

      • Hus


  • Coupon Chamber

    is there a way to get date and time from the network.

    i was trying to build an app using phonegap and jquerymobile. its registration starts on specific timing. So how can i check if the date and time on the mobile correct or not.

    • It’s not possible by using network plugin, but you can use javascript function for datetime

  • Coupon Chamber

    How to check if the phone date and time is correct or not?
    i see this function on whatsapp.

    how they compare the date and time with the server?

  • Eddie Mooney

    Hi Sundaravel,
    Is it possible to create an app to tell the phone to use a certain DNS for both cellular and wifi ?

    • @disqus_UlWsTmpkgV:disqus, I don’t have idea on this

      • Eddie Mooney

        That’s ok Sundara, thank you for prompt reply.