繁体   English   中英

在从服务器加载用于tableview的Plist之前检查Internet

[英]Check Internet before loading Plist for tableview from server

我正在使用带有导航控制器的Storyboard和prepareforsegue。 我有两个UITableViews。 如果单击第一个表中的行,则转到第二个表。 第二个表从plist中选取数据,具体取决于您在第一个表中单击的行。 只要有互联网连接,这种方法就可以正常工作。 如果没有互联网连接,它会崩溃。

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

我想用NSURLConnection做这个,但我不知道在哪里实现代码。 我会将它放在prepareforsegue的第一个表格的.m或第二个表格的.m中吗?

谢谢。

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

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

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

按照目标 - > BuildPhase-> LinkBinaryWithLibraries->单击“+” - >选择SystemConfiguration。

然后在ViewController中导入#import“Reachability.h”标头。

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

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       
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM