Reusing UIWebView is causing crashes

后端 未结 5 1520
礼貌的吻别
礼貌的吻别 2021-01-02 21:46

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

相关标签:
5条回答
  • 2021-01-02 22:07

    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?

    0 讨论(0)
  • 2021-01-02 22:10

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

    0 讨论(0)
  • 2021-01-02 22:21

    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]; 
    }
    
    0 讨论(0)
  • 2021-01-02 22:26

    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.

    0 讨论(0)
  • 2021-01-02 22:26

    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.

    0 讨论(0)
提交回复
热议问题