views:

9793

answers:

3

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.

+28  A: 

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

Andrew Grant
Great tip, thanks!
unforgiven3
excellent. Thanks.
givp
Do you have to assign a value to "SBUsesNetwork" or just add it as a key? I can't find any documentation on it in the iPhone SDK or the Apple Developer Support site :-(
Teflon Ted
They're both boolean's so should be set to true
Andrew Grant
Correct me if I'm wrong, but these settings seem to have limitations. SBUsesNetwork only causes an alert if the phone is in airplane mode. UIRequiresPersistentWiFi only causes an alert if WiFi is enabled but not connected. Neither setting seems to cause an alert if user has disabled WiFi.
Clint Harris
Seems that these flags aren't enough anymore. My app just got rejected with them set. I'll have to take a look at the Reachability demo.
brianegge
Are you sure SBUsesNetwork isn't deprecated or something? I find it hard to believe that to allow an app on App Store, Apple requires the setting of a flag which they don't mention even once anywhere in their documentation...
Psionides
I'm not aware of it's current status, but they don't "require" this flag to be set. It's an optional flag for apps that do not have any form of offline mode. The same way there's a flag for apps that only support landscape mode.
Andrew Grant
Nice tips. Instead of setting in plist, is there any way to change the values dynamically?
Shivan Raptor
@brianegge If you have fixed your app, can you post on how exactly you did it? Thanks
barfoon
@barfoon If the reachability shows a network is available, I open a URL connection to a known location. If this fails, I display a message to the user.
brianegge
+19  A: 

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.

Alex Reynolds
Great. Will try that. Thanks
givp
I made a note about Reachability here: http://stackoverflow.com/questions/181485/determining-when-an-edge-connection-comes-back-after-a-dropout-on-an-iphone
Chris Lundie
A: 

You all are thinking to hard it is only a few lines of code!

- (void)webView:(UIWebView *)NBC didFailLoadWithError:(NSError *)error{
    UIAlertView *alert = [[UIAlertView alloc]
            initWithTitle:@"Connection Error"
            message:@"You have a connection failure. You have to get on a wi-fi or a cell network to get a internet connection."
            delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

That is all that is needed for it to find it you have no connection or not!

Brandon Shaw
This would work only under very specific circumstances, such as when the application is using a UIWebView with the delegate set properly. For *any* other kind of web access (especially "calls to web services" like the OP specified, which very likely *don't* use a UIWebView), it does nothing.
Tim