Skip navigation
GaBling
Currently Being Moderated

Detecting network connection

Jul 31, 2011 9:51 AM

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.

 
Replies
  • Currently Being Moderated
    Aug 1, 2011 5:24 AM   in reply to GaBling

    Hi,

    At present, it is not possible to detect connection on iOS using any AIR API. http://developer.apple.com/library/ios/#documentation/general/Referenc e/InfoPlistKeyReference/Articles/iPhoneOSKeys.html seems to suggest that it should do the job.

     

    -Sanika

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2011 11:11 AM   in reply to GaBling

    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[i].name.toLowerCase() == "wifi" && interfaces[i].active)

     

                    {

     

                        trace("WiFi connection enabled");

     

                        return true;

     

                    }

     

                    else if(interfaces[i].name.toLowerCase() == "mobile" && interfaces[i].active)

     

                    {

     

                        trace("Mobile data connection enabled");

     

                        return true;

     

                    }

     

                }

     

                return false;

     

            }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2011 12:25 PM   in reply to Anurag Vardhan

    Did you try it on iOS? Normally you should call NetworkInfo.isSupported first... and on iOS, it will return false.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2011 10:47 PM   in reply to Rangora

    Ya its work fine in IOS.We deployed the application on IPAD and it works.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 9, 2012 1:55 PM   in reply to Anurag Vardhan

    Has this extension continued to work? I'm constantly told I have no interfaces.length on an iPad2 running OS5.1 over Wifi.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 5:33 AM   in reply to sinious

    Hi Sinious, did you get it working?

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 27, 2012 5:39 AM   in reply to JamieCrow

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 7:16 AM   in reply to GaBling

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 3, 2012 12:43 PM   in reply to Applauz78

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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 30, 2012 10:02 PM   in reply to Rich@Work

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 31, 2012 8:53 AM   in reply to cnickless

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

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 8, 2012 9:59 AM   in reply to Rich@Work

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 10, 2012 10:04 AM   in reply to JamieCrow

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 11, 2012 3:22 AM   in reply to sinious

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points