dev18
January 16, 2015, 7:33am
1
if(window.Connection) {
if(navigator.connection.type == Connection.NONE) {
$ionicPopup.confirm({
title: “Internet Disconnected”,
content: “The internet is disconnected on your device.”
})
.then(function(result) {
if(!result) {
ionic.Platform.exitApp();
}
});
}
}
i have used this code.But this is not working.
For this functionality you have to add the network-connection plugin
ionic plugin add org.apache.cordova.network-information
If you want to listen to the offline event, you can do the following:
document.addEventListener("offline", myCallbackFunction, false);
dev18
January 16, 2015, 9:30am
3
in every controller i need to add this?
dev18
January 16, 2015, 12:51pm
4
still not working can you please tell me.how to use it with example code.
As the documenation sais, you can add this code in your app.run()
function:
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
alert('you are offline');
}
2 Likes
dev18
January 16, 2015, 1:17pm
6
this is not working i have added this code to app.run(). still not working.
I have installed the plugin to my app and everything is working fine.
This is my code:
$ionicPlatform.ready(function() {
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';
console.log('Connection type: ' + states[networkState]);
}
$interval(function(){
checkConnection();
}, 5000)
document.addEventListener("offline", onOffline, false);
function onOffline() {
// Handle the offline event
alert('you are offline');
}
})
I disabled wifi, then 4g and then comes the alert. With chrome://inspect
I get the changes listed from the interval output.
1 Like
dev18
January 16, 2015, 2:02pm
8
plugin is installed . then i copy your code still it is not giving any alert.
1 Like
Do you get any errors? Do you use emulator, or device (which device)? Did you disable any network connection?
dev18
January 16, 2015, 2:09pm
10
there is no error.I am using samsung device.i enable wifi and then disable it it does not give any alert.
what samsung do u have? and wich android version is installed on it? I have tested the code on my Note 4 with android 4.4.4 on it
dev18
January 16, 2015, 2:27pm
12
android 4.2.2 samsung s4 mini
I have tested my code now on Galaxy Note 1 (4.1.2) and S3 Mini (4.3). Both are alerting when I disable network connection.
Maybe you should remove and add android platrom again with ionic.
Some times with cordova plugins, you need to make sure you build the project again after adding the plugin, or else the native project won’t know of the new code.
If that doesn’t work, as @bwasnie1 has suggested, removing/adding the platform should do the trick.
http://ionicframework.com/docs/ionic-cli-faq/#plugins
2 Likes
Robin
January 19, 2015, 1:04am
15
Dont try to reinvent the wheel. Drifty has a project for you out there: ngCordova.
Just install ngCordova in your project and use the $cordovaNetwork Angular service to check network status or the online and offline events being broadcasted on the $rootScope:
$rootScope.$on('$cordovaNetwork:online', function() {});
$rootScope.$on('$cordovaNetwork:offline', function() {});
Works like a charm.
6 Likes
By the way, if you consider your app will be ran in a browser for testing purpose first, you must get know, that Chrome provides such thing with its navigator
object like onLine = !$window.navigator || $window.navigator.onLine
2 Likes
mladen5
January 22, 2015, 9:36pm
17
He is just using it wrong, internet should be disabled before app is launched.
boltex
January 22, 2015, 9:39pm
18
uh… nope.
can disable afrter it’s launched and it detects network is off. tried it multiple time.
mladen5
January 23, 2015, 9:12am
19
It can, but not in this example, because he is probably adding network check code to app.js that is run only once on launch.