【问题标题】:didFailLoadWithError is called with UIWebView even though page later loads即使页面稍后加载,也使用 UIWebView 调用 didFailLoadWithError
【发布时间】:2011-08-11 15:14:12
【问题描述】:

我有一个 UIViewController,它是一个 UIWebViewDelegate,里面有一个 UIWebView。我正在尝试加载特定的 URL

    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    [self.view addSubview:webView];
    [webView release];

但是didFailLoadWithErrordelegate 方法几乎是立即被调用,错误对象是:

Did fail load with error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)" UserInfo=0x1a66c0 {NSErrorFailingURLKey=www.somewebsite.com, NSErrorFailingURLStringKey=www.somewebsite.com}

但是不久之后,您可以看到网站加载正常。

为什么调用fail方法?以及无论网站是否实际失败,我如何知道它何时实际失败以及何时调用该方法?

【问题讨论】:

  • 我遇到了同样的错误,因为我第一次使用 url,第二次加载 url1。也许它会对任何人都有帮助。

标签: iphone xcode ipad uiwebview


【解决方案1】:

当页面通过 javascript 或其他方式立即重定向时,您可能会收到此错误

您可以通过此处的回答来避免显示错误:How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

喜欢

if ([error code] != NSURLErrorCancelled) { //show error alert, etc. }

但我不知道如何识别它被调用的位置。

【讨论】:

  • `if ([error code] != NSURLErrorCancelled) { //显示错误警报等 }' URL 应该全部大写 :)
【解决方案2】:

因此,根据另一个StackOverflow question from a while back,您收到的错误代码是由在前一个请求完成之前发出的请求引起的。

可能发生的情况是您同时/非常快速地拨打多个电话,其中一个成功(因此您的网页加载)但另一个失败。

【讨论】:

    【解决方案3】:

    在 web 视图中使用 TapGesture,点击计数为 1。 使用处理TapGesture的函数,检查URL是否返回状态码200,如果是,调用下面的函数

    -(void)animateme{
        NSString *urlAddress = Connecting_URL
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [self.webview loadRequest:requestObj];
    }
    

    并且,使用委托函数,

    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        UIAlertView  *alert = [[UIAlertView alloc] initWithTitle:@"Device in offline" message:@"Please check your internet connection" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alert show];
    }
    

    希望对你有帮助:)

    【讨论】:

    • 这不能回答问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多