1

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.

IssamTP
  • 2,408
  • 1
  • 25
  • 48

1 Answers1

4

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.

Community
  • 1
  • 1
makdad
  • 6,402
  • 3
  • 31
  • 56
  • Thank you. That's interesting. I've partially solved this problem using Reachability only in the first View to check if the user has the connection (whole app is based on data downloaded from Internet). Using NSURLConnection then, gives me the chance to check if there are blocking error and prevent crashes. – IssamTP Jan 06 '11 at 12:05
  • Those are some words of wisdom. I struggled with reachability checking and getting the wrong results for the longest time. It makes sense to check reachability status only after failure of some network related request. – wasabi Jun 21 '12 at 20:07