2012-07-22 40 views
0

我想显示的提示信息时,用户没有互联网连接我用下面的代码,但它从来没有进入到这一块它永远不会触发它从来没有所谓iOS版 - 连接失败的提示信息时,在我的应用程序没有互联网连接

如何控制用户是否有网络连接?

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"connect"); 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:@"check your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alert show]; 

} 

注:我使用的iOS 5.1版本,我已经把我的代表们到.h文件

+0

确保这个方法是在你的NSURLConnection的 – Vatev 2012-07-22 14:44:39

+0

的代表,我有代表:NSURLConnectionDataDelegate,NSURLConnectionDelegate – ercan 2012-07-22 14:49:25

+0

https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference .html它在NSURLConnectionDelegate中。 – hkulekci 2012-07-22 14:49:37

回答

2

尝试Reachability类检查互联网连接的可用性。它比connection:(NSURLConnection *)connection didFailWithError快得多,当没有互联网连接时,通常需要1分钟才能启动此方法。但是,连接切换后,可达性block将立即触发。

+0

谢谢,我试图使用Reachability类。我添加.m和.h文件,然后如何在我的xib文件中使用它?在viewDidLoad中,我如何使用Reachability Class来检查可用性? – ercan 2012-07-22 15:19:04

+0

看到这篇文章。 http://stackoverflow.com/questions/3790957/reachability-guide-for-ios-4 – rakeshNS 2012-07-22 15:22:29

+0

感谢它现在的作品与可达性 – ercan 2012-07-22 15:39:58