I have an App that uses UITabBar and it has to download contents from the Internet, so I decided to use the class Reachability. When I launch it, the method works greatly, but if I don't wait that all the job is done and I go to another tabBar index, then I go back to the first one, the App holds on and doesn't move. Here's some code:
- (void)viewWillAppear:(BOOL)animated {
[[self.navigationController navigationBar] setHidden:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
[internetReachable startNotifier];
[hostReachable startNotifier];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
BOOL flag;
UIAlertView *alert;
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
alert = [[UIAlertView alloc] initWithTitle:@"Attenzione!" message:@"Non ci sono connessioni disponibili a internet: impossibile scaricare i dati!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
switch ( internetStatus ) {
case NotReachable:
self.internetActive = NO;
flag = NO;
break;
case ReachableViaWiFi:
self.internetActive = YES;
flag = YES;
break;
case ReachableViaWWAN:
self.internetActive = YES;
flag = YES;
break;
}
if ( flag )
[NSThread detachNewThreadSelector:@selector(loadDataFromInternet) toTarget:self withObject:nil];
else {
[alert show];
[self.spinner stopAnimating];
}
[alert release];
}
I'll paste everything else you may need.
I had a similar problem with an app. This is also similar to this question, which I just answered - make sure you're checking asynchronously, not on the main thread (or at least not blocking the UI).
Also, interestingly I've read a resource that suggests that when you need Internet access, just go for it. Don't use Reachability first for "preflight". Use Reachability after you've failed to determine why you've failed :). I recall that piece of wisdom being from Apple itself - but I forget where I read it and a quick Google isn't finding it.