2013-05-14 76 views
1

在我的启动画面中,我订阅了UIApplicationDidBecomeActiveNotification。检查Internet连接iOS App

- (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]; 
    } 
} 

的问题是,互联网连接将只在启动屏幕上检查,所以如果我在其他一些画面中,我失去互联网连接那么有没有办法告诉大家,连接已不见了。我怎样才能做出一个好的逻辑来检查客户的互联网连接。

+0

你使用的是webview。 – 2013-05-14 16:47:11

+0

希望这下面的链接是有用的.. [http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk] [1] [1]:http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk – 2013-05-14 16:50:03

回答

1

您想要使用可访问性库,以便获取有关更改的回调(like this one)。

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

0

我会建议创建一个类,所有的互联网请求过滤,然后在该类中检查连接性(使用可达性建议)。如果你这样设计,它应该简化一点。