views:

267

answers:

3

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 WebView 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;
}
A: 

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.

mips
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
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.
mips
+1  A: 

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];
fluchtpunkt
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 ...
testing
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.
fluchtpunkt
A: 

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!

CopyAndPaster