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.

4

1 回答 1

4

我的应用程序也有类似的问题。这也类似于我刚刚回答的这个问题- 确保您正在异步检查,而不是在主线程上(或至少不阻塞 UI)。

另外,有趣的是,我读过一个资源,它建议当您需要访问 Internet 时,就去做吧。不要首先将可达性用于“预检”。在您无法确定失败的原因后使用可达性:)。我记得那段智慧来自苹果本身——但我忘记了我在哪里读到的,而且快速的谷歌也找不到它。

于 2011-01-05T23:16:51.110 回答