Apple 类的可达性问题

发布于 2024-10-10 06:27:38 字数 1382 浏览 0 评论 0原文

我有一个使用 UITabBar 的应用程序,它必须从互联网下载内容,所以我决定使用 Reachability 类。当我启动它时,该方法效果很好,但如果我不等待所有工作完成并转到另一个 tabBar 索引,然后返回第一个,应用程序将保持不变。这是一些代码:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-10-17 06:27:38

我在一个应用程序中遇到了类似的问题。这也类似于这个问题,我刚刚回答了这个问题 - 确保您正在异步检查,而不是在主线程上检查(或者至少不会阻塞 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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文