场景很简单,我的应用程序在激活时会查找信息。大多数情况下,它只会使用缓存,但在某些情况下,它需要 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)。如果仍然没有连接可用,则显示一条错误消息。对实现此目标的简单实现有何建议?
最佳答案
尝试使用 Reachability
的回调 block ,如
this answer .
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];
关于ios - 应用程序需要等待互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864438/