ios - 整个应用程序为 "Handle No Internet"

标签 ios ios6 reachability

我正在开发一个适用于iOS6.0和>的应用程序。它是基于选项卡的导航 Controller 应用程序

我说10 UIViewControllers每个 View Controller 都需要互联网才能工作。

现在我的问题是处理没有互联网问题的最佳方法是什么?此外,如果互联网连接可用,则应用程序必须再次正常工作。

附注我完全知道Reachability类(class)。但我不想在所有 10 个 View Controller 中设置可达性更改通知。

必须有某种方法来做到这一点,这意味着无论我是什么 View Controller ,它都会显示“无互联网 View ”,并且当互联网恢复时它会像往常一样工作。类似“当互联网不存在时呈现无互联网 View ”或类似内容。但不确定如何?

虽然不确定,但我听说 Apple 提供了一些在应用程序顶部显示“无互联网”消息的功能,并且除非互联网恢复,否则不允许用户导航。

我需要完全相同的。

任何成功的指南都将非常感激。

最佳答案

我会选择稍微不同的方法。为什么不创建一个 UIViewController 子类来为您处理互联网连接通知?你可以做类似这个半伪代码的事情。我只是凭空写下了这段代码。它可能包含错误。所以不要只是复制和粘贴。

@interface SMInternetBaseViewController : UIViewController {
    SMOverlay* overlay;
}
@end

@interface SMInternetBaseViewController()
- (void)reachabilityChanged:(NSNotification *) not;
@end

@implementation SMInternetBaseViewController
- (id)init {
    self = [super init];
    if (self) {
        // Register here the method reachabilityChanged for Reachability notifications 
    }
    return self;
}

- (void)reachabilityChanged:(NSNotification *) not
{
    // Define here how to behave for different notifications

    if (__NO_INTERNET__) {
        // Add an overlay to the view.
        if (!overlay) {
            overlay = [[SMOverlay alloc] init];
        }
        [self.view addSubview:overlay];
    }

    if (__AGAIN_INTERNET__) {
        [overlay removeFromSuperview];
    }

}
@end

然后您可以轻松地使所有 View Controller 成为此 SMInternetBaseViewController 的子类,并且您不必再关心它。

关于ios - 整个应用程序为 "Handle No Internet",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17995597/

相关文章:

objective-c - UIWebView 委托(delegate)获取 MIME 类型

ios - 从 ACAccountStore/ACAccount 检索 Facebook token 过期?

ios - wifi 已连接但互联网不可用 iOS

ios - UITableView 在 UICollectionView 中

objective-c - UITextfield 未在 uitableviewcell 中扩展

ios - +entityForName : nil happens intermittently

ios - 从 UIViewController 过渡到另一个

iphone - 崩溃: UIImage style crash with code for iOS 6?

ios - 如何 Hook ios 连接更改

iphone - 可达性reachabilityWithAddress不起作用