ios - 应用程序需要等待互联网连接

标签 ios objective-c

场景很简单,我的应用程序在激活时会查找信息。大多数情况下,它只会使用缓存,但在某些情况下,它需要 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/

相关文章:

ios - 异常说发送到一个不符合 "frame"属性的 KVC 的对象

ios - 在 SpriteKit 中使用加速度计移动和旋转 Sprite

ios - iPhone 上的准确时间

ios - 在运行时切换Firebase配置文件

iphone - 使用 NSCalendar 检索每月的第一天

ios - UIAlertController 被窗口上的 View 覆盖

ios - 从聊天室中过滤掉不合适的词

objective-c - 方法实现末尾的分号

iOS 使用正则表达式拆分 NSString

objective-c - iphone-sdk,在多个 View 之间共享数据