How we can get network information using phonegap

Posted by Prayag Ch
3
Jan 29, 2020
737 Views
Image
Hello Friends, Today we will discuss, how to get network information using PhoneGap / Apache Cordova. Here we’re going to use network plugin ( https://github.com/apache/cordova-plugin-network-information ). It provides information about the device’s cellular and wifi connection, and whether the device has an internet connection.

if you want to know to get network information using PhoneGap / Apache Cordova, then,

You have to follow these STEP

Step 1:

Create a 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 org.apache.cordova.network-information

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 getting 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.

Thanks for reading,

You can also read my blog how to crack an interview, here you can readable and some tips that will definitely help you to crack any  interview level
3 people like it
avatar avatar avatar
Comments
avatar
Please sign in to add comment.