Checking for network connectivity
I am using the code below in an attempt to notify users that they are not connected to a network. The problem is, my alertDialog box doesn't show up in the simulator when I test my app with no internet connectivity. Is there something missing from my code? I am using it in app.js
Full Code: http://pastie.org/1066272
// check for network
if(!Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){
var alertDialog = Titanium.UI.createAlertDialog({
title: 'WARNING!',
message: 'Your device is not online.',
buttonNames: ['OK']
});
alertDialog.show();
}
8 Answers
-
Accepted Answer
You got it backward
if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){
// that's no network connectivity.
}if(Titanium.Network.networkType != Titanium.Network.NETWORK_NONE){
anything but no connectivity
} -
I use this:
function check_network() { return Titanium.Network.online; }
and works even in simulator ( I cut the airport )
-
I've always just loaded it onto my device and went in airport mode after the sync.
I'd love to know if it's possible in the simulator.
-
Actually now it is showing the alertDialog when I am connected to a network.
Any suggestions?
-
Tamas, I replaced your code with mine.
I am not getting an alert pop-up with your code, should I be seeing an alert that tells me I am not connected to a network? Thanks
-
Tamas, I replaced your code with mine.
I am not getting an alert pop-up with your code, should I be seeing an alert that tells me I am not connected to a network? Thanks
-
Thanks Daniel, I found the fix with your solution.
// check for network if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){ var alertDialog = Titanium.UI.createAlertDialog({ title: 'WARNING!', message: 'Your device is not online.', buttonNames: ['OK'] }); alertDialog.show(); }
-
Thanks guys, added a new post here:
http://www.learningtitanium.com/snippets/how-to-check-for-a-internet-connection.html