Ignoring NSURLErrorDomain error -999 does not work in UIWebView

寵の児 提交于 2019-11-30 15:25:28

问题


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?


回答1:


From Apple docs:

NSURLErrorCancelled (-999)

"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."

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.

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.




回答2:


Most of the time when working with NSURLConnection or UIWebView, this error is due to a timeout. Might really not be your code, but your connectivity.



来源:https://stackoverflow.com/questions/13860401/ignoring-nsurlerrordomain-error-999-does-not-work-in-uiwebview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!