9

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;
}

6 Answers 6

28

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!

Sign up to request clarification or add additional context in comments.

4 Comments

this did not solve my problem for whatever reason. i'm guessing it's a bug in the API.
Thanks: "removeAllCachedResponses" did not work for me but this did.
What about if I'm using loadHtmlString? How can I clear the cache every time I loadHtmlString?
Only with [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; and [_webView loadRequest:request]; my UIWebView did not reload the page. I had to add [_webView reload];
10

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.

3 Comments

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.
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.
Perfect!! This is the only solution on the internet that i found to be working!!
4

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

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Comments

2

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];

2 Comments

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 ...
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.
2

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.)

Comments

0

You could also load @"about:blank" after the webview disappears, then it will be empty the next time you access it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.