Skip to main content
July 31, 2011
Question

Detecting network connection

  • July 31, 2011
  • 15 replies
  • 12805 views

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.

This topic has been closed for replies.

15 replies

Inspiring
June 28, 2012

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 //

Participating Frequently
July 3, 2012

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";

Participating Frequently
August 31, 2012

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?

Participating Frequently
August 1, 2011

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

August 2, 2011

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!

Participating Frequently
August 4, 2011

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;

        }