Network detection on the iPhone
Hi people
I may be being a bit thick but is there a way you can determine if the iPhone has a signal or is wirelessly connected.
I am running the HTTPclient to obtain a list of tweets and using the normal get(), onload(), send() methods. My main concern was if someone trys to run it with no signal or wireless conecttion is there a method that would achieve this.
Steve
4 Answers
-
Simple way, with an AlertDialog popup:
var network = Titanium.Network; // NETWORK CONNECTION CHECK if (network.online == false) { Ti.UI.createAlertDialog({title:'No Cellular Data Network', message:'Please connect to a cellular data network.', buttonNames:['Close']}).show(); }
-
Hii,
Im sure theres a better way of doing it.. But the only function ive found so far checks wether there is a network connection or not. You could be connected to WIFI but have no internet connection, or on 3G but have run out of credit so cannot access the web but it will still let you know theres a network connection (Well.. Thats because there is! Would be cool if there was a check to see wether it can connect to the web).. Anyway, i use the below! As you can see i use xhr.onload and xhr.onerror
xhr.open('POST','XXX'); xhr.send({ugcImage:imageUpload, ugcAudio:audioUpload, ugcName:nameTextField.value, ugcEmail:emailTextField.value, ugcMessage:messageTextArea.value,ugcLatitude:latitude, ugcLongitude:longitude, ugcAccuracy:accuracy, ugcStation:station}); var sendingDialog = Titanium.UI.createAlertDialog({ message: 'Sending message…' }); sendingDialog.show(); setInterval(function(){ sendingDialog.hide(); },3000); xhr.onload = function xhrSuccess(){ sendingDialog.hide(); var responseText = xhr.responseText; if(responseText == "true") { alert("Message has been sucesfully sent."); sent = true; sendBtn.title = 'Message Sent'; } else { alert("Message was unsuccessful in sending. Please try again."); sent = false; } }; xhr.onerror = function xhrError(){ sendingDialog.hide(); alert("Message was unsuccessful in sending. Please try again."); sent = false; };
-
The right way to do it is:
if ( Titanium.Network.online ) { // do ur stuff } else { alert( "This application requires internet connection.") }
This is what Apple recommends and if you fail to warn the user that needs internet for some functions will reject your app.
-
Titanium.Network.online