DedrisFrameworkDedrisFramework

← Plugins  /  Network Information

📶

Network Information

cordova-plugin-network-information
iOS Android 🌐 Browser

This Apache Cordova plugin lets your app find out whether the device is currently online and what kind of connection it is using, and it lets you respond the moment connectivity changes. Use it to warn the user when they go offline, to defer heavy downloads to Wi-Fi, or to retry requests as soon as the network comes back.

Installation

Add the plugin to your Cordova project from the command line:

terminal
cordova plugin add cordova-plugin-network-information

The connection type property

Once the plugin is ready, read navigator.connection.type to learn how the device is connected. The value is always one of the connection constants below:

ConstantMeaning
Connection.UNKNOWNA connection exists but its type could not be determined.
Connection.ETHERNETThe device is connected over a wired Ethernet link.
Connection.WIFIThe device is on a Wi-Fi network.
Connection.CELL_2GA second-generation cellular connection.
Connection.CELL_3GA third-generation cellular connection.
Connection.CELL_4GA fourth-generation cellular connection.
Connection.CELLA cellular connection of an unspecified generation.
Connection.NONEThe device has no connection — it is offline.

Events

The plugin fires two events on the document object so you can react to connectivity changes as they happen. Add the listeners after deviceready:

EventFires when
onlineThe device gains a connection.
offlineThe device loses its connection.

Usage example

Watch for connectivity changes and inspect the current connection type:

www/js/app.js
document.addEventListener("deviceready", onReady, false);

function onReady() {
  document.addEventListener("online", function() {
    console.log("Back online — type: " + navigator.connection.type);
  }, false);

  document.addEventListener("offline", function() {
    console.log("Connection lost");
  }, false);

  if (navigator.connection.type === Connection.NONE) {
    console.warn("Starting offline — some features may be unavailable");
  }
}

Tips

📚 Source: the technical reference on this page (the connection property, constants, events and the install command) is based on the official Apache Cordova network-information documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project