Hello Readers,
This plugin provides the information about the device network connection (Cellular or Wifi) or detects a device has internet connection or not.
To use this functionality first of all we need to install the following plugin through CLI:
$ cordova plugin add cordova-plugin-network-information
The following properties describe the state and type of the network connection :
- Connection.UNKNOWN
- Connection.ETHERNET
- Connection.WIFI
- Connection.CELL_2G
- Connection.CELL_3G
- Connection.CELL_4G
- Connection.CELL
- Connection.NONE
Full Example:
js code:
function checkConnectionType() {
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]);
}
checkConnectionType();
In the above code the navigator.connection object provides the network information.
We can define network related event i.e detects when device is offline or online.
Offline event fires when connection.type is NONE or device goes to offline mode.
Now the document.addEventListener attach an event listener when the deviceready event fires.
For Example :
document.addEventListener("offline", onOffline, false);
function onOffline()
{
alert('Device is offline');
}
Online event fires when device connected to the network.
For Example:
document.addEventListener("offline", onOnline, false);
function onOnline()
{
alert('Device is online');
}
For more details click here
Hope this will help you :)
0 Comment(s)