How to delete the cache from UIWebview or dealloc UIWebview

32,702

Solution 1

My solution to this problem was to create the UIWebView programmatically on viewWillAppear: and to release it on viewDidDisappear:, like this:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.webView = [[[UIWebView alloc]initWithFrame:exampleFrame] autorelease];
    self.webView.scalesPageToFit = YES;
    self.webView.delegate = self;
    self.webView.autoresizingMask = webViewBed.autoresizingMask;
    [exampleView addSubview:self.webView];
}
- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self.webView removeFromSuperview];
    self.webView.delegate = nil;
    self.webView = nil;
}

If you do this and your UIWebView doesn't get released you should check it's retain count and understand who is retaining it.

Solution 2

I had nearly the same problem. I wanted the webview cache to be cleared, because everytime i reload a local webpage in an UIWebView, the old one is shown. So I found a solution by simply setting the cachePolicy property of the request. Use a NSMutableURLRequest to set this property. With all that everything works fine with reloading the UIWebView.

NSURL *url = [NSURL fileURLWithPath:MyHTMLFilePath]; 
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
 [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
 [self.webView loadRequest:request];

Hope that helps!

Solution 3

If you want to remove all cached responses, you may also want to try something like this:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Solution 4

I did something simpler, I just set the webView's alpha to 0 on loadRequest and 1 on webViewDidFinishLoad.

thewebview.alpha=0.0;
thewebview.backgroundColor=[UIColor whiteColor];
[thewebview loadRequest:[NSURLRequest requestWithURL:url(str)]];

and

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    thewebview.alpha=1.0;
}

(Actually I have a fade-in, as rendering f.ex. a PDF can take 1/5 seconds or so.)

Solution 5

If I had to make a guess your property for webView is

@property (nonatomic, retain) IBOutlet UIWebView *webView;

Correct? The important part is the retain.

self.webView = [[UIWebView alloc]initWithFrame:frame];

The above code allocates a UIWebView (meaning retainCount = 1) and then adds it to self.webView, where it is retained again (retainCount = 2).

Change the code to the following, and your webView should be deallocated.

self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];

addSubview: can only be called on UIViews.

//[self.navigationController addSubview:self.webView];

should be

[self.navigationController.view addSubview:self.webView];
Share:
32,702
testing
Author by

testing

Updated on April 05, 2020

Comments

  • testing
    testing about 4 years

    Every time I load a new page with UIWebView the before loaded page is shown for a short time.

    How can I clear that cache? Another possibility would be to dealloc UIWebview. I tried that but than my UIWebView is always "empty". How should the alloc and dealloc be done in this case?

    I noticed that the UIWebView is consuming about 10 MB RAM. Now the UIWebView is loaded together with the ViewController. And the view is autoreleased as well as the UIWebView is autoreleased. Wouldn't it be better to dealloc the WebView each time?

    Solution:

    - (void) viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        CGRect frame = CGRectMake(0, 0, 320, 480);
        self.webView = [[[UIWebView alloc]initWithFrame:frame] autorelease];
        self.webView.scalesPageToFit = YES;
        [self.view addSubview:self.webView];
    }
    
    - (void) viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
    
        [self.webView removeFromSuperview];
        self.webView = nil;
    }
    
  • testing
    testing over 13 years
    What is exampleFrame, webViewBed and exampleView? What is autoresizingMask? I don't implement the UIWebViewDelegate protocol so I don't need the delegate. addSubview don't work for me, because my UIViewController does not support this. As far as I can see you are not releasing the web view on viewDidDisappear. You only remove it from the superview and set it to nil. I tried your approach and I only can see a white screen. I edited my question so you can see my code.
  • testing
    testing over 13 years
    You're right. But I don't understand that why the retain count of 2 causes the webview to display a white screen. Thank you for your help. Whom should I give the check mark? It's difficult ...
  • Matthias Bauch
    Matthias Bauch over 13 years
    I don't know what or even if the white screen has something to do with this issue. But if you have memory management issues left it's complicated to debug other problems. So first fix the memory management and investigate further into the white screen issue.
  • mips
    mips over 13 years
    exampleFrame is just the size of the UIWebView you are creating. webViewBed shouldn't be a part of this example, it's not useful in this context. exampleView is the view you're adding the UIWebView to. And of course, you addSubview()s to Views, not to view controllers. So you'll have to do something like [viewController.view addSubview:...]. I am releasing the webview, it's a property (see the "self."?) so you can set it to nil and it'll be released.
  • markshiz
    markshiz over 12 years
    this did not solve my problem for whatever reason. i'm guessing it's a bug in the API.
  • user621267
    user621267 almost 12 years
    Thanks: "removeAllCachedResponses" did not work for me but this did.
  • Bagusflyer
    Bagusflyer almost 12 years
    What about if I'm using loadHtmlString? How can I clear the cache every time I loadHtmlString?
  • Juan T
    Juan T almost 11 years
    Only with [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; and [_webView loadRequest:request]; my UIWebView did not reload the page. I had to add [_webView reload];
  • Ravikant
    Ravikant over 7 years
    Perfect!! This is the only solution on the internet that i found to be working!!