My iPhone app requires internet to function.
Should I checks for connectivity everytime a internet function is executed? Or should I use my appDelegate to listen for messages from something like the reachability class to determine if the connection was lost and warn the user?
What is better?
Here's what I've done so far in my app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
reachability = [[Reachability reachabilityWithHostName:[NSString stringWithFormat:@"http://%@/iSql/" ,[[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]]] retain];
[reachability startNotifier];
return YES;
}
- (void)reachabilityChanged:(NSNotification *)note {
NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];
if (ns == NotReachable) {
if (![networkAlert isVisible]) {
if ([self networkAlert] == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to retrieve data from the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[self setNetworkAlert:alert];
[alert release];
}
[networkAlert show];
}
} else {
if ([self networkAlert] != nil) {
[networkAlert dismissWithClickedButtonIndex:0 animated:YES];
}
}
}