Reusing UIWebView is causing crashes

2020-02-29 01:02发布

I've read that reusing UIWebViews is kind of a bad practice. Some code I inherited at work is pushing a variety of content to a UIWebView. Powerpoints, Word documents, and video. This all works just fine under normal circumstances. When we get to switching out the content in the UIWebView too fast, it dumps.

My webView is set as a property. It is hooked up in IB just fine. Normal selection from our tableView loads local content just fine. It takes rapid fire selection of either the same cell or combinations of multiple to get it to crash.

I can capture some error messages for it for the webViewDidFailWithError. But those will trigger even without a crash. Here is the error localized string.

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

When the app does finally crash, it blows up on this goofy WebCore error.

Application Crash image

If anyone has any links or some code examples how to handle this I would appreciate it. Maybe an example how to best reuse my webView property without blowing things up.

I would load some of my code, but there is a lot going on not related to the webView itself. All content being pushed to the webView is done via [self.webView loadRequest:request]; with the request being an NSURLRequest filled with the path to the local content.

I will be very appreciative if anyone can help me out on this one. Fingers crossed for something simple.

5条回答
Juvenile、少年°
2楼-- · 2020-02-29 01:16

In your UIWebViewDelegate, you could implement webView:shouldStartLoadWithRequest:navigationType: to return NO if there is already a request loading.

查看更多
Viruses.
3楼-- · 2020-02-29 01:20

Are you using [webview stopLoading]; before loading another request? What you might need to do is cancel or stop the current loading before trying to load a different one. The other option being restrict the user input.

查看更多
男人必须洒脱
4楼-- · 2020-02-29 01:25

I'm not certain this way is the "best" way to solve the issue, but it does seem to be working quite well. Short, sweet, and it works.

I disabled userInteraction with the tableView that updates the content in the webView. Since you have to mash on it so much, missing a tap here or there probably won't be missed. So for now, this is the fix.

#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]; 
}
查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-29 01:25

I can't help but think you'd be better off not re-using the UIWebView. Ditch the nib, create it programmatically and set it to nil and re-create/re-assign/re-alloc it when the data source changes.

On a different note I would make sure to use NSOperationQueue for the data loading.

查看更多
Rolldiameter
6楼-- · 2020-02-29 01:37

If it’s crashing only when you tap quickly, could you put a gesture recognizer over it to act as a poor man's rate limiter?

查看更多
登录 后发表回答