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开发者_Python百科 have read that they rejects apps because of this issues.
Thank You,
Regards,
Dilshan
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. :)
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.
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.
精彩评论