15 Replies Latest reply on Oct 11, 2012 3:22 AM by JamieCrow

    Detecting network connection


      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.

        • 1. Re: Detecting network connection
          sanika Kulshreshtha Adobe Employee


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



          1 person found this helpful
          • 2. Re: Detecting network connection
            GaBling Level 1

            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!



            • 3. Re: Detecting network connection
              Anurag Vardhan Level 1

              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();








                              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;



              • 4. Re: Detecting network connection
                Rangora Level 3

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

                • 5. Re: Detecting network connection
                  Anurag Vardhan Level 1

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

                  • 6. Re: Detecting network connection
                    sinious Most Valuable Participant

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

                    • 7. Re: Detecting network connection

                      Hi Sinious, did you get it working?

                      • 8. Re: Detecting network connection
                        sinious Most Valuable Participant

                        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.

                        • 9. Re: Detecting network connection
                          Applauz78 Level 2

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



                          // This function is called when internet connection status has changed

                          function statusChanged(event:StatusEvent) {


                          trace("my status is: "+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





                          // END OF SOLUTION FOR INTERNET CONNECTION TEST //

                          • 10. Re: Detecting network connection
                            Rich@Work Level 1

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

                            • 11. Re: Detecting network connection
                              cnickless Level 1

                              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?

                              • 12. Re: Detecting network connection
                                Rich@Work Level 1

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

                                • 13. Re: Detecting network connection
                                  JamieCrow Level 1

                                  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?

                                  • 14. Re: Detecting network connection
                                    sinious Most Valuable Participant

                                    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?

                                    • 15. Re: Detecting network connection
                                      JamieCrow Level 1

                                      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.