1

在我的初始屏幕中,我订阅了 UIApplicationDidBecomeActiveNotification。

 - (void)viewDidLoad
{
    [super viewDidLoad];


    // register notifications

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkInternetConnection:) name:UIApplicationDidBecomeActiveNotification object:nil];

}

-(void) checkInternetConnection:(NSNotification *) notification
{
    internetStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];

    if(internetStatus == NotReachable)
    {
        [AppHelper showAlert:@"ERROR" message:@"Error downloading data. Please check your network connection and try again." cancelButtonTitle:nil otherButtonTitles:@[@"OK"]];
        return;
    }

    [self viewDidAppear:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if(!(internetStatus == NotReachable))
    {
        AppDelegate *applicationDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        [applicationDelegate performSelector:@selector(showMainMenu) withObject:[NSNumber numberWithBool:YES] afterDelay:3.0];
    }
}

问题是互联网连接只会在初始屏幕上检查,所以如果我在其他屏幕中间并且我失去了互联网连接,那么就无法判断连接已经消失。如何制定一个好的逻辑来检查客户端的互联网连接。

4

2 回答 2

1

您想使用一个可访问性库,它允许您获取有关更改的回调(例如这个)。

然后,在每个需要它的控制器中,创建一个可达性实例并设置reachableBlockandunreachableBlock以执行所需的操作。

于 2013-05-14T15:47:25.427 回答
0

我建议创建一个所有互联网请求都过滤的类,并检查该类中的连接性(使用建议的可达性)。如果您以这种方式构建它,它应该会简化一些事情。

于 2013-05-14T16:28:29.850 回答