Reputation: 869
I am trying to figure out if the device is connected to the internet when the app is started. As far as I have read, the javascript code should be something like this:
function isOnline() {
var networkState = navigator.network.connection.type;
if (networkState == Connection.NONE) {
return false;
};
return true;
}
I have only tried it on iOS so far. Unfortunately, navigator.network is undefined.
I know that I have to add the network module in the config.xml file. According to the PhoneGap page (http://docs.phonegap.com/en/3.0.0/cordova_connection_connection.md.html), this is the way I did it:
<feature name="NetworkStatus">
<param name="ios-package" value="CDVConnection" />
</feature>
<feature name="NetworkStatus">
<param name="android-package" value="org.apache.cordova.NetworkManager" />
</feature>
Does anybody know what I am missing?
Upvotes: 1
Views: 1858
Reputation: 23883
try this one.
install this cordova plugin here.
https://github.com/apache/cordova-plugin-network-information
and add this code into js file
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
isOnline();
}
function isOnline() {
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';
if ((states[networkState]) == states[Connection.NONE]) {
alert('No Internet Connection. Click OK to exit app');
navigator.app.exitApp();
}
}
Upvotes: 2
Reputation: 731
try this one:
function checkConnection() {
return navigator.connection.type === "none" || navigator.network.connection.type === null ||
navigator.connection.type === "unknown" ? false : true;
}
And remember to check this AFTER device ready event has fired.
Upvotes: 0