views:

31

answers:

3

Hi,

My iPhone application continuously communicate with a web server. So in case of a connection lost or the server is not reachable is there any specific precautions we should do? I know that we have to show the user about the communication failure. If so is there any acceptable way of showing errors to the user? Im more concentrated on apple guidelines where I have read that they rejects apps because of this issues.

Thank You,

Regards,

Dilshan

+1  A: 

You should check if the server is reachable before you send a request and if an error occured or the server is unreachable you can show an UIAlertView to inform the user.

Pierre
Do we need to check internet connectivity/wifi in the phone?
Dilshan
I dont know if you must do it but i think its better. You can make it with this method http://github.com/erica/iphone-3.0-cookbook-/blob/master/C13-Networking/01-Connectivity/main.m
Pierre
- (BOOL) connectedToNetwork
Pierre
I am using ASIHTTPRequest and if the failure handling part it already do this. But not sure whether this is sufficient.
Dilshan
Im not familiar with ASIHTTP but it looks like you have only to implement the ASIHTTPRequestDelegate protocol. You can try if your error handling is good enough if you start a reqeust to a non existing URL and switch your phone to flight mode. The flight mode is what you need to catch because Apple tests your app in it.
Pierre
ok thank you for your reply Pierre
Dilshan
+1  A: 

Just use the apple Reachability class. Here is an example on how to use it and you can get the class from the apple documentation here. This is all you need to check the reachability and add a listener to your app to update if you lose the connection to the server.

Once you have the Reachability class in your project, all you have to do is something like this:

//Change the host name here to change the server your monitoring

hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReach startNotifier];


internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];

wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];

NetworkStatus netStatus = [internetReach currentReachabilityStatus];

if (netStatus == NotReachable){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Connection Not Found" message:@"Need network connection present to operate."
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

You will also need a method that fires if the reachability changes. That tutorial will explain how to do that if you are not familiar. Simple as that. :)

Geoff Baum
Just realized I may have misread the question, but hopefully this still helps.
Geoff Baum
Thanks for the reply Geoff. This is the answer I expected. But any chance of degrading the performance due to the above check?
Dilshan
It shouldn't. When you add the listener in the appDidFinishLaunching method (or wherever you decide to add it) that will intercept any reachability changes. Nothing executes unless you receive a reachability notification.
Geoff Baum
+1  A: 

I've found it's better to just try hitting the network rather than checking Reachability since Reachability can lock up for 20+ seconds in the DNS lookup and NSURLDownload can sometimes bring up the network if it's down.

You should still use Reachability, however, to retry when the network comes back online after being down.

Whether you should display an error to the user depends on your application, but most of the time "yes". In my applications I only show one network error every 30 seconds or so, though, to avoid annoying the user, even though I have dozens of outstanding requests that can all fail.

EricS
Interesting. Seems like this is an additional check? Without the pre reachability check cant we consider a timeout?
Dilshan
Yes, I use a timeout with the NSURLConnection, typically 30-90 seconds. Seems to work well for simple HTTP GET requests. Looks like if you set the http body Apple may change your timeout, though. See https://devforums.apple.com/message/108292
EricS