开发者

How to delete the cache from UIWebview or dealloc UIWebview

开发者 https://www.devze.com 2023-01-16 03:22 出处:网络
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 possibilit开发者_JS百科y would be to dealloc UIWebview. I tried that

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 possibilit开发者_JS百科y 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;
}


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!


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.


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

[[NSURLCache sharedURLCache] removeAllCachedResponses];


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消