Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to check network information in phonegap

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 480
    Comment on it

    If you want to check the network information in your code, here is the plugin that cordova provides to check if the user is online or offline. This plugin gives the information of cellular or wifi connection of the device and it can also check if the device has an internet connection or not.

    Here is the process how you can implement this plugin into your code:

    Installing the plugin:

    To install this plugin, you have to run the following command:

    cordova plugin add cordova-plugin-network-information

    Platforms that are supported for this plugin includes:

    • Amazon Fire OS
    • Android
    • BlackBerry 10
    • Browser
    • iOS
    • Windows Phone 7 and 8
    • Tizen
    • Windows

    How to check connection:

    There is an object called connection that can provide information about the cellular or wifi connection of device. It can be exposed via navigator.connection method. It has property connection.type that is used to determine the connection state.


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

    There are events related to network:

    offline: When the application is offline i.e. device is not connected to internet, this event fires. It means when a device loses internet connection you can start this event.

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

    online: When the application is online i.e. device is connected to internet, this event fires.

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

    This is how you can check the network state in your application.


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: