Problems with Reachability Apple class

不打扰是莪最后的温柔 提交于 2019-12-13 02:38:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4584275/problems-with-reachability-apple-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!