【问题标题】:iOS - connection fail alert message when no internet connectioniOS - 没有互联网连接时的连接失败警报消息
【发布时间】:2012-07-22 14:40:32
【问题描述】:

在我的应用程序中,我想在用户没有互联网连接时显示一条警报消息,我使用了下面的代码,但它从未进入此块它永远不会触发它从未调用过

我如何控制用户是否有互联网连接?

-(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 文件中

【问题讨论】:

标签: iphone ios ios5 nsurlconnection


【解决方案1】:

尝试Reachability 类来检查互联网连接的可用性。它比connection:(NSURLConnection *)connection didFailWithError 快得多,通常在没有 Internet 连接时触发此方法需要 1 分钟。但是一旦连接切换,可达性block 就会触发。

【讨论】:

  • 谢谢,我尝试使用可达性类。我添加了 .m 和 .h 文件,那么如何在我的 xib 文件中使用它?在 viewDidLoad 我如何使用可达性类来检查可用性?
猜你喜欢
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 2016-11-03
相关资源
最近更新 更多