I have followed this article:
to get network information of the device in ionic app for android.
It works fine on browser but when I install the compiled apk in android phone, it gives an error that says Reference error : Connection is not defined.
at line where I use $cordovaNetwork.isOnline();
I have been banging my head around and have done my due research and tried uninstalling and installing it in the order suggested but no help.
Help me fix this issue. This problem is probably not an issue with code and may be need some clever fix to get it working.
This same issue is being discussed here but I have not really understood where is that given piece of code coming from.
index.html :
<!DOCTYPE html>
<html ng-app="starter" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<!-- <link href="lib/ionic/css/angular-datepicker.min.css" rel="stylesheet"> -->
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- angular date picker css-->
<link href="lib/datePicker/css/angular-pickadate.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic/js/highcharts-ng.js"></script>
<script src="lib/ionic/js/jquery.min.js"></script>
<script src="lib/ionic/js/highcharts.js"></script>
<script src="lib/ionic/js/ngStorage.min.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-controller="LoginCtrl">
<ion-nav-bar class="mob-bar-balanced">
<!-- <ion-nav-back-button>
</ion-nav-back-button> -->
</ion-nav-bar>
<ion-nav-view></ion-nav-view>
</body>
</html>
app.js:
app.factory('ConnectivityMonitor', ['$rootScope', '$cordovaNetwork', function($rootScope, $cordovaNetwork){
return {
isOnline: function(){
if(ionic.Platform.isWebView()){
$rootScope.online = $cordovaNetwork.isOnline();
return $cordovaNetwork.isOnline();
} else {
$rootScope.online = navigator.onLine;
return navigator.onLine;
}
},
isOffline: function(){
if(ionic.Platform.isWebView()){
$rootScope.online = $cordovaNetwork.isOnline();
return !$cordovaNetwork.isOnline();
} else {
$rootScope.online = navigator.onLine;
return !navigator.onLine;
}
},
startWatching: function(){
if(ionic.Platform.isWebView()){
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
$rootScope.online =true;
console.log("went online");
});
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
$rootScope.online =false;
console.log("went offline");
});
}
else {
window.addEventListener("online", function(e) {
$rootScope.online =true;
console.log("went online");
}, false);
window.addEventListener("offline", function(e) {
$rootScope.online =false;
console.log("went offline");
}, false);
}
}
}
}]);
.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('Login',{
url:'/login',
onEnter:["$state","$localStorage", '$rootScope' , '$ionicViewSwitcher',function($state,$localStorage, $rootScope, $ionicViewSwitcher){
if((typeof($localStorage.userInfo)!== 'undefined') && (Object.keys($localStorage.userInfo).length !== 0)) {
$ionicViewSwitcher.nextTransition('none');
$state.go("Deployment");
}
}],
templateUrl:'templates/login.html',
controller:'LoginCtrl',
resolve: {
online: function(ConnectivityMonitor){
return ConnectivityMonitor.isOnline();
}
}
})