Copy link to clipboard
Copied
I'm developing an app for IOS which stores data on a web server and thus does not work offline. So I need to detect if connection is available (also required for app approval). I've found some solutions for AIR by googling but they don't seem to be available for IOS.
So how can I detect connection on IOS?
I've set UIRequiresPersistentWiFi in the app's xml to YES but it doesn't do the job, it only detects if airplane mode is on.
Copy link to clipboard
Copied
Hi,
At present, it is not possible to detect connection on iOS using any AIR API. http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html seems to suggest that it should do the job.
-Sanika
Copy link to clipboard
Copied
Thank you Sanika! Do you know if a specific documentation for Air for IOS is planned so that it will be easier to determine which AIR functions are actually available?
I tried the UIRequiresPersistentWiFi Key but again, it seems to only detect if 3G/WIFI is on not if it's actually available, since it only activates when in airplane mode.
I guess I'll have to just warn when connection to the server fails, without specifying network connection isn't available.
Thanks again!
Copy link to clipboard
Copied
The below function is used to check for Internet Connection in Flex for all devices.
You also have to listen to change event of NetworkInfo so that in middle of the application if internet is disconnected then that case is handled.
private function checkInternetConnection():Boolean
{
var interfaces:Vector.<NetworkInterface> = NetworkInfo.networkInfo.findInterfaces();
if(interfaces.length==0)
{
return true;
}
for(var i:uint = 0; i < interfaces.length; i++)
{
if(interfaces.name.toLowerCase() == "wifi" && interfaces.active)
{
trace("WiFi connection enabled");
return true;
}
else if(interfaces.name.toLowerCase() == "mobile" && interfaces.active)
{
trace("Mobile data connection enabled");
return true;
}
}
return false;
}
Copy link to clipboard
Copied
Did you try it on iOS? Normally you should call NetworkInfo.isSupported first... and on iOS, it will return false.
Copy link to clipboard
Copied
Ya its work fine in IOS.We deployed the application on IPAD and it works.
Copy link to clipboard
Copied
Has this extension continued to work? I'm constantly told I have no interfaces.length on an iPad2 running OS5.1 over Wifi.
Copy link to clipboard
Copied
Hi Sinious, did you get it working?
Copy link to clipboard
Copied
I haven't tried it since. I'm more interested in knowing not only if I have net (which can be known via standard AS3 classes returning errors when you try to access the net), but if I suddenly lose it. Once the native extension reports an event for loss and gain of connectivity I might give it a try again.
Copy link to clipboard
Copied
I am using the following to test for an internet connection.
/// SOLUTION FOR CHECKING FOR INTERNET CONNETION ////
var testURL:String = "http://www.google.com";
var testRequest:URLRequest = new URLRequest(testURL);
var urlCheck:URLMonitor = new URLMonitor(testRequest);
urlCheck.addEventListener(StatusEvent.STATUS, statusChanged);
urlCheck.start();
// This function is called when internet connection status has changed
function statusChanged(event:StatusEvent) {
trace("my status is: "+urlCheck.available);
if(urlCheck.available){
trace("You have an internet connection, all is good");
}
else {
// Launch Notification informing user connection is lost and some features may not work
setNetworkStatus();
}
}
// END OF SOLUTION FOR INTERNET CONNECTION TEST //
Copy link to clipboard
Copied
We were using this to test for an internet connection as well but there is a unique situation in which this will fail.Specifically if the iPad is connected to a wireless network without internet. We have an internal wifi network which we connect our tablets to in order to access local shares, when used on this network the StatusEvent.STATUS event is never called, in addition to Applauz78's code a timer should also be created; if the StatusEvent listener is not called within some timeout period then you could assume there is no internet connectivity. Also to ensure your UrlRequest would download as fast as possible and in turn verify internet connectivity as fast as possible you could modify the url request to only download the Header rather than the whole webpage, This can be accomplished with the following.
testRequest.method = "HEAD";
Copy link to clipboard
Copied
Thanks Rich, that's a helpful addition.
When you mention the timer, are you talking something long enough to allow for the header to download from Google (i.e. 5 seconds is plenty)?
Then once the StatusEvent first fires that the header was downloaded, kill the timer and let the URLMonitor take over?
Copy link to clipboard
Copied
What I was doing is calling the URLMonitor's start function and a timers start function at the same time, for the timer I have lowered the timeout period to 2 seconds, the header is small and should come back right away, For the apps I have been doing its been more important to show the content even if it is outdated than to confirm an internet connection and get the new content, so if the vice versa is more important for your app increase the timeout to what you think is adequate.
The StatusEvent is on the URLMonitor not the Timer, if the StatusEvent function is triggered you can kill the timer because the URLMonitor is working correctly, if the timer's timeout function is triggered then you can kill the URLMonitor because you are in that special scenario which I have mentioned. Another situation you can is if the timeout function is triggered just run your app as if there is no internet connection (since there is none) but leave the URLMonitor running in the background. I am not sure about memory usage if this is the case though, the benefit to this would be that if at any time an internet connection is added while the app is running the URLMonitor's StatusEvent will return and you can add logic to enable your internet connected features.
In my experiences though if I had no internet connection when the app was turned on then I did not want it enabled later, your requirements may differ..
Copy link to clipboard
Copied
Has anyone tried this scenario...
Start app
Check connection is OK (in app)
connect to BTOpenZone (which you are not logged into)
Confirm that connection is no longer OK (in app)
Change to wirless network that you know works
I find that the network isn't detected again without restarting the app, even if you call URLMonitor.start again.
Any ideas? Do other people get the same result?
Copy link to clipboard
Copied
If you attempt to load content in any way that you know to exist online and get an error back about not being able to load it, that's a fairly reliable way of knowing if you're online or not.
The reason I mention this is the internet is not perfect. Just because URLMonitors check on google.com who has a huge CDN comes back as "internet available", it doesn't mean your server is. Your server could be in an area with an outage, your server could be down or simply overloaded. Knowing you're online is one thing. Knowing your services are available is different. You may need a more robust checking method.
Otherwise, are you nulling your URLMonitor and recreating it to re-check net?
Copy link to clipboard
Copied
Thanks for the reply. I haven't tried nulling it each time, but if you call URLMonitor.stop each time before
URLMonitor.start it seems to do the job. (not too surprisingly!) I'll look into nulling it too but if I can reuse it instead of recreating it it would be preferable.