How to safely shut down a loading UIWebView in viewWillDisappear?

前端 未结 6 1212
温柔的废话
温柔的废话 2020-12-07 11:34

I have a view containing a UIWebView which is loading a google map (so lots of javascript etc). The problem I have is that if the user hits the \'back\' button on the nav ba

6条回答
  •  自闭症患者
    2020-12-07 12:07

    A variation on this should fix both the leaking and zombie issues:

    - (void)loadRequest:(NSURLRequest *)request
    {
        [self retain];
        if ([webView isLoading])
            [webView stopLoading];
        [webView loadRequest:request];
        [self release];
    }
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
        [self retain];
    }
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        [self release];
    }
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    {
        [self release];
    }
    
    - (void)viewWillDisappear
    {
        if ([webView isLoading])
            [webView stopLoading];
    }
    
    - (void)dealloc
    {
        [webView setDelegate:nil];
        [webView release];
        [super dealloc];
    }
    

提交回复
热议问题