xcode 即使页面稍后加载,也会使用 UIWebView 调用 didFailLoadWithError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7028383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:42:50  来源:igfitidea点击:

didFailLoadWithError is called with UIWebView even though page later loads

iphonexcodeipaduiwebview

提问by CodeGuy

I have a UIViewController which is a UIWebViewDelegate and has a UIWebView inside of it. I am trying to load a particular URL

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

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

But the didFailLoadWithErrordelegate method is almost instantly called, and the error object is:

但是didFailLoadWithError委托方法几乎是立即调用的,错误对象是:

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}

However a short time after, you can see that the website loads just fine.

然而不久之后,您可以看到网站加载得很好。

Why is the fail method being called? And how do I know when it has actually failed versus when the method was called regardless of if the website actually failed or not?

为什么会调用失败方法?以及我如何知道它实际失败的时间与调用方法的时间,而不管网站是否实际失败?

回答by Marek Sebera

You can get this error when the page is instantly redirected via javascript or somehow else

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

You can avoid showing the error by answer from here: How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

您可以通过以下答案避免显示错误:How do I fix NSURLErrorDomain error -999 in iPhone 3.0 OS

like

喜欢

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

but I don't know how to recognize where it was invoked.

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

回答by lxt

So according to another StackOverflow question from a while back, the error code you're getting is being caused by a request being made before the previous request has been completed.

因此,根据不久前的另一个StackOverflow 问题,您得到的错误代码是由在前一个请求完成之前发出的请求引起的。

What may be happening is you are making multiple calls at the same time / very rapidly, and one is succeeding (so your web page loads) but the other is failing.

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

回答by Pradheep Narendran

Use TapGesture on the web view using tapcount of 1. Using a function for handling TapGesture, check if the URL returns status code of 200, if so, call the below function

在 web 视图上使用 TapGesture 使用 tapcount 为 1。使用处理 TapGesture 的函数,检查 URL 是否返回状态代码 200,如果是,则调用以下函数

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

And, using delegate function,

并且,使用委托函数,

-(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];
}

Hope it helps :)

希望能帮助到你 :)