Reusing UIWebView Crashes

I read that reusing UIWebViews is bad practice. Some of the code I inherited at work pushes a variety of content to UIWebView. Powerpoints, Word documents and videos. All of this works fine under normal circumstances. When we switch content too quickly in UIWebView, it is unloaded.

My webView is set as a property. It is connected to IB just fine. The usual selection from our tableView loads local content just fine. It requires quick selection of the fire of one or the same cell or several combinations to make it fail.

I can write some error messages for webViewDidFailWithError. But this will happen even without a crash. Here is a localized error string.

The operation couldn't be completed. (NSURLErrorDomain error -999.) 

When the application finally crashes, it explodes on this stupid WebCore error.

Application Crash image

If anyone has any links or code examples on how to handle this, I would appreciate it. Perhaps an example of how best to use my webView property without blowing things in.

I would download part of my code, but a lot is not happening in relation to the webView itself. All content passed to the webView is executed using [self.webView loadRequest:request]; with the request being NSURLRequest filled out with the local content path.

I will be very grateful if anyone can help me with this. Fingers crossed for something simple.

+6
source share
5 answers

I'm not sure if this method is the β€œbest” way to solve the problem, but it seems to work very well. Short, sweet, and it works.

I disabled userInteraction with a tableView that updates the content in the webView. Since you need to brake so hard that there is no pass here, or there probably will not be missed. So, this is a correction.

 #pragma mark - #pragma mark UIWebViewDelegate methods -(void)webViewDidStartLoad:(UIWebView *)webView { [self.tableView setUserInteractionEnabled:NO]; } -(void)webViewDidFinishLoad:(UIWebView *)webView { [self.tableView setUserInteractionEnabled:YES]; } // I re-enable on load failures as they can block you out entirely on fail -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self.tableView setUserInteractionEnabled:YES]; } 
+2
source

If it crashes only with a quick press, could you put a gesture recognizer on it to act as a limiter with limited ratios?

+1
source

Do you use [webview stopLoading]; before loading another request? You may need to cancel or stop the current download before trying to download another one. Another parameter restricts user input.

0
source

In UIWebViewDelegate, you can implement webView:shouldStartLoadWithRequest:navigationType: to return NO if the download is already loaded.

0
source

I cannot help but think that you better not use UIWebView. Rotate the tip, create it programmatically and set it to zero, and recreate / reassign / redistribute it when the data source changes.

In another note, I would definitely use NSOperationQueue to load the data.

0
source

Source: https://habr.com/ru/post/908186/


All Articles