简体   繁体   English

如何从UIWebview或dealloc UIWebview中删除缓存

[英]How to delete the cache from UIWebview or dealloc UIWebview

Every time I load a new page with UIWebView the before loaded page is shown for a short time. 每次我使用UIWebView加载新页面时,会在很短的时间内显示之前加载的页面。

How can I clear that cache? 我该如何清除缓存? Another possibility would be to dealloc UIWebview . 另一种可能性是取消分配UIWebview I tried that but than my UIWebView is always "empty". 我尝试过但是我的UIWebView总是“空”。 How should the alloc and dealloc be done in this case? 在这种情况下如何进行allocdealloc

I noticed that the UIWebView is consuming about 10 MB RAM. 我注意到UIWebView消耗大约10 MB RAM。 Now the UIWebView is loaded together with the ViewController . 现在, UIWebViewViewController一起加载。 And the view is autoreleased as well as the UIWebView is autoreleased. 视图是自动释放的, UIWebView是自动释放的。 Wouldn't it be better to dealloc the WebView each time? 每次取消分配WebView不是更好吗?

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. 我希望清除webview缓存,因为每次我在UIWebView重新加载本地网页时,都会显示旧的。 So I found a solution by simply setting the cachePolicy property of the request. 所以我通过简单地设置请求的cachePolicy属性找到了一个解决方案。 Use a NSMutableURLRequest to set this property. 使用NSMutableURLRequest设置此属性。 With all that everything works fine with reloading the UIWebView . 通过重新加载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: 我解决这个问题的方法是在viewWillAppear:以编程方式创建UIWebView viewWillAppear:并在viewDidDisappear:上释放它viewDidDisappear: ,如下所示:

- (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. 如果您这样做并且您的UIWebView没有被释放,您应该检查它的保留计数并了解谁保留它。

如果要删除所有缓存的响应,您可能还想尝试这样的事情:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

If I had to make a guess your property for webView is 如果我不得不猜测你的webView属性是

@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). 上面的代码分配了一个UIWebView(意思是retainCount = 1),然后将它添加到self.webView,再次保留它(retainCount = 2)。

Change the code to the following, and your webView should be deallocated. 将代码更改为以下内容,并且应取消分配您的webView。

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

addSubview: can only be called on UIViews. addSubview:只能在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. 我做了一些更简单的事情,我只是在loadRequest上将webView的alpha设置为0,在webViewDidFinishLoad上设置为1。

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.) (实际上我有一个淡入,因为渲染f.ex.一个PDF可能需要1/5秒左右。)

您也可以在webview消失后加载@“about:blank”,然后在您下次访问时它将为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM