Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK Internet connection detection

I'm working on an iPhone application that makes a few calls to web services. I posted this application on the Apple store but it got rejected (and rightly so) since there was no error message displayed to the user if no Internet connection is available. Since obviously the application would not work without it.

So I just wanted to know how to best achieve this? I'm guessing something needs to go in the viewDidLoad method that will throw an alert box saying something like "You need an Internet connection to use this application".

Any ideas would be appreciated.

like image 867
givp Avatar asked Feb 27 '09 20:02

givp


People also ask

How does iOS check Internet connection?

To check if your iOS mobile device is connected to Wi-Fi:From the main screen of your device, look for and open Settings. With Settings open, look for the Wi-Fi field. Off - the Wi-Fi antenna is currently disabled. Not Connected - Wi-Fi is turned on, but your device is not currently connected to a network.

How does NWPathMonitor check Internet connection?

You can use the function usesInterfaceType(_:) to check which interface type this network path uses. This is the most effective way to figure out if your app is connected over WiFi, cellular or ethernet. print(“It's WiFi!”)


2 Answers

If your application must have network access the easiest way is to add the following settings to your info.plist as boolean values.

  • SBUsesNetwork - Ensure the device has an active connection (Edit: not applicable, this seems to be a private API someone found at some point. It is not in Apple's developer documentation.)
  • UIRequiresPersistentWiFi - Ensures the device is connected via WiFi

If your choice is not true then the user will be presented with an appropriate message when starting your application. Best of all this message is from the OS and thus is localized.

If your application cannot download data from a website while running (loss of signal, site down) you should still warn the user though and not just spin indefinitely.

like image 173
Andrew Grant Avatar answered Sep 30 '22 19:09

Andrew Grant


Apple Developer Connection has a sample application (Reachability) that uses the System Configuration framework to determine network status. It will tell you whether you have a WiFi, EDGE/3G or no Internet connection.

You would use portions of this code in your application to determine network state, and then provide interface cues if no connection is available, such as a UIAlertView.

like image 27
Alex Reynolds Avatar answered Sep 30 '22 18:09

Alex Reynolds