Reputation: 67
I try to check if internet is working when I run the app. It works well on my computer but after compiling apk and running the app on my smartphone, this code doesn't work properly; could you help me ?
.run(function($window, $rootScope) {
console.log($cordovaNetwork.getNetwork());
$rootScope.online = navigator.onLine;
$window.addEventListener("offline", function() {
$rootScope.$apply(function() {
$rootScope.online = false;
});
}, false);
$window.addEventListener("online", function() {
$rootScope.$apply(function() {
$rootScope.online = true;
});
}, false);
})
regards
Upvotes: 1
Views: 405
Reputation: 159
You can Use cordova network Plugin(Cordova Network Plugin). Use can use the code below
.run(function($ionicPlatform, $ionicPopup) {
$ionicPlatform.ready(function() {
// Check for network connection
if(window.Connection) {
if(navigator.connection.type == Connection.NONE) {
$ionicPopup.confirm({
title: 'No Internet Connection',
content: 'Sorry, no Internet connectivity detected. Please reconnect and try again.'
})
.then(function(result) {
if(!result) {
ionic.Platform.exitApp();
}
});
}
}
});
})
Upvotes: 0
Reputation: 77930
Its not good practice to use navigator.onLine
on Android devices. Each device uses different version of Chromium and on some devices it will not work as expected.
I suggest you to use org.apache.cordova.network-information plugin that calls native API for network information (advantage)
Upvotes: 1