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

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

问题描述

我正在尝试避免在 UIWebView 的委托时返回错误时产生的问题。我在委托实现中有共同的解决方法(我在互联网上的任何地方看到过)

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文档:


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.

为了解决这个问题,我建议将以下跟踪添加到您的网络视图代理中:

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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆