忽略 NSURLErrorDomain 错误 -999 在 UIWebView 中不起作用

Ignoring NSURLErrorDomain error -999 does not work in UIWebView(忽略 NSURLErrorDomain 错误 -999 在 UIWebView 中不起作用)

本文介绍了忽略 NSURLErrorDomain 错误 -999 在 UIWebView 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免在 UIWebView 的委托返回这样的错误时产生的问题.我的委托实现中有常见的解决方法(我在 Internet 的任何地方都看到过)

I'm trying to avoid the problem generated while UIWebView's delegate returns an error like that. I have the common workaround (I saw it anywhere in the Internet) in my delegate implementation

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

    if ([error code] == NSURLErrorCancelled) return;
}

我遇到的问题是这并不总是有效.有时会加载网页,有时会加载网页的某些部分(标题、文本的一部分……),有时不会加载任何内容.

The problem I have is that this does not works always. Sometimes loads the web, another times loads parts of the web (the header, a part of the text...) and several times does not load anything.

还有其他解决方案吗?是否存在任何可以正常工作的浏览器的开源实现?

Is there any other solution to this? Exists any open source implementation of a browser that works properly?

推荐答案

来自 Apple 文档:

From Apple docs:

NSURLErrorCancelled (-999)

NSURLErrorCancelled (-999)

" 取消异步加载时返回.Web Kit 框架委托在对加载资源执行取消操作时将收到此错误.请注意,如果下载被取消,NSURLConnection 或 NSURLDownload 委托将不会收到此错误."

"Returned when an asynchronous load is canceled. A Web Kit framework delegate will receive this error when it performs a cancel operation on a loading resource. Note that an NSURLConnection or NSURLDownload delegate will not receive this error if the download is canceled."

因此,最有可能发生这种情况的情况是,在第一个请求完成之前,您先加载一个请求,然后再加载另一个请求(或同一个请求).这可能发生.例如,如果您在像 viewDidAppear: 这样可以多次调用的方法中调用 loadRequest(或 loadHTMLString).如果您快速点击 UIWebView 中的 2 个链接,也会发生这种情况.

So, the most likely case for this to happen is for you to load a request and then another one (or the same one), before the first one is completed. This can happen. e.g., if you call loadRequest (or loadHTMLString) in a method like viewDidAppear: that can be called multiple times. This has also been reported to happen if you quickly tap 2 links in the UIWebView.

因此,一般建议是查看您调用 loadRequest(或 loadHTMLString)的方式和位置,并可能提供一些代码.

So, the general suggestion is to review how and where you call loadRequest (or loadHTMLString), and possibly provide some code.

为了解决这个问题,我建议将以下跟踪添加到您的 Web 视图委托:

In order to troubleshoot this, I would suggest to add the following traces to your web view delegate:

- (void)webViewDidStartLoad:(UIWebView *)webView {
      NSLog(@"Starting to download request: %@", [webView.request.URL absoluteString]);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
      NSLog(@"Finished downloading request: %@", [webView.request.URL absoluteString]);
}

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

    if ([error code] == NSURLErrorCancelled)
      NSLog(@"Canceled request: %@", [webView.request.URL absoluteString]);
}

如果您检查输出,您应该更清楚地看到发生了什么.如果您粘贴输出,我们可以尝试进一步帮助您.

If you inspect the output, you should see more clearly what is going on. If you paste the output, we could try and help you further.

这篇关于忽略 NSURLErrorDomain 错误 -999 在 UIWebView 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:忽略 NSURLErrorDomain 错误 -999 在 UIWebView 中不起作用

基础教程推荐