Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing UIWebView is causing crashes

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.

like image 241
Bill Burgess Avatar asked Feb 10 '12 22:02

Bill Burgess


2 Answers

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]; 
}
like image 116
Bill Burgess Avatar answered Nov 09 '22 04:11

Bill Burgess


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?

like image 25
Ellen Teapot Avatar answered Nov 09 '22 02:11

Ellen Teapot