【问题标题】:didFailLoadWithError incorrectly reports no connectiondidFailLoadWithError 错误地报告没有连接
【发布时间】:2013-02-01 17:43:37
【问题描述】:

我有一个带有标准视图控制器的应用程序,上面有多个按钮。每个按钮都链接到具有唯一 UIWebView 的单独视图控制器。每个 UIWebView 都实现了 didFailLoadWithError 并且它似乎工作正常:当我关闭 wifi 并尝试从主视图控制器页面加载 UIWebView 时,我正确地从 didFailLoadWithError 得到错误消息。当我打开 wifi 并加载 UIWebView 时,它工作正常 - 没有错误。但是,当我单击该 UIWebView 页面中的链接时,我再次收到 didFailLoadWithError 错误。更有趣的是,我清除了错误消息,新页面仍然从我刚刚单击的链接加载,所以我知道连接良好。这是我的实现...有谁知道强制 didFailLoadWithError 在第一次加载时只运行一次并在您验证网络连接良好后禁止它再次运行的方法?

@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Alert"    message:@"No Internet Connection - Please Check Your Network Settings and Try Again" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:     @"http://www.site.com/index.html"]]];
    [webView addSubview:activity];
    timer=[NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
        target:self selector:@selector(loading) userInfo:nil repeats:YES];
           }

- (void)loading {
    if (!webView.loading)
        [activity stopAnimating];
        else
            [activity startAnimating];
}

【问题讨论】:

  • 我对此进行了更多测试,发现问题不在代码或 didFailLoadWithError 例程中,而是由特定链接引起的。出于某种原因,所有从 UIWebView 跟踪以下链接的尝试都将导致 didFailLoadWithError,但所有“正常”链接都不会导致错误。这是有问题的链接:p.incmedia.incmediaservices.netdna-cdn.com/vod/…
  • 我也遇到了同样的问题,找到解决办法了吗?

标签: uiwebview


【解决方案1】:

我刚遇到这个问题。发生了什么,当您单击 Web 视图中的链接时,当页面仍在加载时,您将收到错误 -999。这转换为NSURLErrorCancelled

您可以在以下链接中找到更多信息。转到URL Loading System Error Codes 部分。 https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html

在我的代码中,我告诉一个警报视图弹出,说在调用-webView:didFailLoadWithError: 时互联网连接丢失。我将该代码包装在错误对象的条件周围。这是一个例子。

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if ([error code] == NSURLErrorNotConnectedToInternet || [error code] == NSURLErrorNetworkConnectionLost) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Check internet connection." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2012-01-16
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多