0

场景很简单,我的应用程序会在它激活时查找信息。大多数情况下,它只会使用缓存,但在某些情况下,它需要 Internet 连接来询问所需的信息。

我使用了 Reachability ( https://github.com/tonymillion/Reachability ) 来确定是否有可用的活动连接。问题是 iPhone 在一段时间处于非活动状态后需要几秒钟才能激活连接。这意味着应用程序看到没有可用的连接并将显示错误消息。

我想要发生的是应用程序将首先检查是否有可用的连接:

Reachability *reachability = [Reachability reachabilityWithHostname:URL_LOTTOTALL_WEB];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus == NotReachable) {
} else {
}

如果没有可用的连接,我想在几秒钟后重试(可能是 2 或 3)。如果仍然没有可用的连接,则显示错误消息。对实现这一目标的简单实现有什么建议吗?

4

1 回答 1

3

尝试使用此答案Reachability中描述 的回调块。

internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Yayyy, we have the interwebs!");
    });
};

// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Someone broke the internet :(");
    });
};

[internetReachableFoo startNotifier];
于 2013-04-07T16:10:23.467 回答