← Plugins / Network Information
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.
Add the plugin to your Cordova project from the command line:
cordova plugin add cordova-plugin-network-information
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:
| Constant | Meaning |
|---|---|
Connection.UNKNOWN | A connection exists but its type could not be determined. |
Connection.ETHERNET | The device is connected over a wired Ethernet link. |
Connection.WIFI | The device is on a Wi-Fi network. |
Connection.CELL_2G | A second-generation cellular connection. |
Connection.CELL_3G | A third-generation cellular connection. |
Connection.CELL_4G | A fourth-generation cellular connection. |
Connection.CELL | A cellular connection of an unspecified generation. |
Connection.NONE | The device has no connection — it is offline. |
The plugin fires two events on the document object so you can react to connectivity changes as they happen. Add the listeners after deviceready:
| Event | Fires when |
|---|---|
online | The device gains a connection. |
offline | The device loses its connection. |
Watch for connectivity changes and inspect the current connection type:
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");
}
}
navigator.connection.type after deviceready has fired — the value isn't reliable before then.online and offline events to keep your UI in sync as connectivity comes and goes.