苹果级可达性问题 [英] Problems with Reachability Apple class

查看:72
本文介绍了苹果级可达性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用UITabBar的应用程序,它必须从Internet下载内容,因此我决定使用Reachability类.当我启动它时,该方法非常有效,但是如果我不等所有工作都完成而又转到另一个tabBar索引,那么我回到第一个索引,该应用程序将继续运行并且不会移动.这是一些代码:

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.

推荐答案

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

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).

此外,有趣的是,我阅读了一份资源,建议您在需要Internet访问时就去找它.不要先将可达性"用于预检".在确定失败的原因后,使用Reachability :).我记得那是来自苹果公司本身的智慧-但我忘记了在哪里阅读它,很快谷歌就找不到了.

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.

这篇关于苹果级可达性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆