I have implemented the following code to determine whether my Cordova application is online or offline.
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown';
states[Connection.ETHERNET] = 'Ethernet';
states[Connection.WIFI] = 'WiFi';
states[Connection.CELL_2G] = 'Cell2G';
states[Connection.CELL_3G] = 'Cell3G';
states[Connection.CELL_4G] = 'Cell4G';
states[Connection.CELL] = 'Cellgeneric';
states[Connection.NONE] = 'Nonetwork';
alert(states[networkState]);
if(states[networkState]!='Nonetwork'){
online=true;
}else{
online=false;
}
Below is an example of how my Angular controller is structured:
.controller('MainCtrl',['$scope','$http','$localStorage','$state',function($scope, $http, $localStorage, $state){
if(online==true){
//code for online
}else{
// code for offline
}
}])
I have called this check within the 'deviceready' event, but I am encountering an issue where deviceready
is triggered after the controller has started execution. Is there a way to verify the network status before the Angular controller begins running?