在从服务器加载 tableview 的 Plist 之前检查互联网

hal*_*y4b 2 iphone nsurlconnection nsurlrequest

我将 Storyboard 与导航控制器和 prepareforsegue 一起使用。我有两个 UITableViews。如果您单击第一个表格中的一行,您将进入第二个表格。第二个表根据您在第一个表中单击的行从 plist 中选取其数据。只要有互联网连接,这就可以正常工作。如果没有互联网连接,它会崩溃。

现在我想在加载第二个表之前检查是否有互联网连接。如果没有互联网连接,我想显示一个 UIAlertView。

我想用 NSURLConnection 做到这一点,但我不知道在哪里实现代码。我会将它放在 prepareforsegue 的第一张桌子的 .m 中,还是放在第二张桌子的 .m 中?

谢谢。

Kam*_*had 5

您应该使用 Apple 编写的可达性代码。

请通过此链接下载可达性文件。

要使用该代码,您需要导入 SystemConfiguration 框架。

按照 As Target->BuildPhase->LinkBinaryWithLibraries->点击"+"->Choose SystemConfiguration。

然后在 ViewController 中导入 #import "Reachability.h" Header。

然后在导航到另一个视图之前编写几行代码

Reachability *reach = [Reachability reachabilityForInternetConnection]; 
NetworkStatus netStatus = [reach currentReachabilityStatus];    
if (netStatus == NotReachable) {        
    NSLog(@"No internet connection!");
    UIAlertView *information = [[UIAlertView alloc] initWithTitle:@"Server Connection is not available" message:nil  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [information show];
    [information release];


} 
else {        
//Write your logic here Like As navigating to anotherview       
}
Run Code Online (Sandbox Code Playgroud)