public
Created

How to properly remove an instance of UIWebView and avoid memory leaks

  • Download Gist
DestroyWebview.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Destroy UIWebView
- (void)destroyWebView
{
[self.webView loadHTMLString:@"" baseURL:nil];
[self.webView stopLoading];
[self.webView setDelegate:nil];
[self.webView removeFromSuperview];
[self setWebView:nil];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
 
// UIWebViewDelegate Methods
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}
 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}

Is it still the case ?
Why do you use webView:shouldStartLoadWithRequest:navigationType: ?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.