清除 UIWebview 缓存 [英] Clearing UIWebview cache
问题描述
我已经使用 UIWebview 使用 loadRequest:
方法加载网页,当我离开那个场景时,我调用 [self.webView stopLoading];
并释放 webView.
I have used UIWebview to load a web page using loadRequest:
method, when I leave that scene I call [self.webView stopLoading];
and release the webView.
在首次启动时的活动监视器中,我看到实际内存增加了 4MB,而在多次启动/加载时,实际内存没有增加.它只增加了一次.
In activity monitor on first launch i have seen that the real memory increased by 4MB, and on multiple launches/loading the real memory doesn't increase. It is increasing only once.
我已经检查了 webview 的保留计数.它是正确的,即 0.我认为 UIWebView 正在缓存一些数据.如何避免缓存或删除缓存数据?还是另有原因?
I have checked the retain count of webview. It is proper i.e., 0. I think UIWebView is caching some data. How do I avoid caching or remove cached data? Or is there another reason for this?
推荐答案
我实际上认为当您关闭 UIWebView
时它可能会保留缓存的信息.我尝试从我的 UIViewController
中删除一个 UIWebView
,释放它,然后创建一个新的.当我返回某个地址时,新的可以准确记住我所在的位置,而无需重新加载所有内容(它会记住我之前的 UIWebView
已登录).
I actually think it may retain cached information when you close out the UIWebView
. I've tried removing a UIWebView
from my UIViewController
, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView
was logged in).
所以有几个建议:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
这将删除特定请求的缓存响应.还有一个调用将删除在 UIWebView
上运行的所有请求的所有缓存响应:
This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView
:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
之后,您可以尝试删除任何与 UIWebView
相关联的 cookie:
After that, you can try deleting any associated cookies with the UIWebView
:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
Swift 3:
// Remove all cache
URLCache.shared.removeAllCachedResponses()
// Delete any associated cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
这篇关于清除 UIWebview 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!