ios 清除 UIWebview 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5468553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing UIWebview cache
提问by Chandan Shetty SP
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.
我已经使用 UIWebview 使用loadRequest:
方法加载网页,当我离开那个场景时,我调用[self.webView stopLoading];
并释放 webView。
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.
在首次启动时的活动监视器中,我看到实际内存增加了 4MB,而在多次启动/加载时,实际内存没有增加。它只增加一次。
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?
我已经检查了 webview 的保留计数。它是正确的,即 0。我认为 UIWebView 正在缓存一些数据。如何避免缓存或删除缓存数据?或者还有其他原因吗?
回答by groomsy
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).
我实际上认为当您关闭UIWebView
. 我试过UIWebView
从我的 中删除 a UIViewController
,释放它,然后创建一个新的。当我返回一个地址时,新的确切地记住了我所在的位置,而无需重新加载所有内容(它记住了我以前的UIWebView
登录)。
So a couple of suggestions:
所以有几个建议:
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
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
:
这将删除特定请求的缓存响应。还有一个调用将删除在 上运行的所有请求的所有缓存响应UIWebView
:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
After that, you can try deleting any associated cookies with the UIWebView
:
之后,您可以尝试删除任何关联的 cookie UIWebView
:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
Swift 3:
斯威夫特 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)
}
}
回答by ToddH
Don't disable caching completely, it'll hurt your app performance and it's unnecessary. The important thing is to explicitly configure the cache at app startup and purge it when necessary.
不要完全禁用缓存,它会损害您的应用程序性能,而且没有必要。重要的是在应用程序启动时显式配置缓存并在必要时清除它。
So in application:DidFinishLaunchingWithOptions:
configure the cache limits as follows:
所以在application:DidFinishLaunchingWithOptions:
配置缓存限制如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];
// ... other launching code
}
Once you have it properly configured, then when you need to purge the cache (for example in applicationDidReceiveMemoryWarning
or when you close a UIWebView
) just do:
正确配置后,当您需要清除缓存时(例如在applicationDidReceiveMemoryWarning
a中或关闭时UIWebView
),只需执行以下操作:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
and you'll see the memory is recovered. I blogged about this issue here: http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
你会看到内存恢复了。我在这里写了关于这个问题的博客:http: //twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/
回答by InsertWittyName
You can disable the caching by doing the following:
您可以通过执行以下操作禁用缓存:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
回答by CodeOverRide
Swift 3.
斯威夫特 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)
}
}
回答by PnotNP
I could not change the code, so I needed command line for testing purpose and figured this could help someone:
我无法更改代码,因此我需要命令行进行测试,并认为这可以帮助某人:
Application specific caches are stored in ~/Library/Caches/<bundle-identifier-of-your-app>
, so simply remove as below and re-open your application
应用程序特定的缓存存储在 中~/Library/Caches/<bundle-identifier-of-your-app>
,因此只需按如下方式删除并重新打开您的应用程序
rm -rf ~/Library/Caches/com.mycompany.appname/
rm -rf ~/Library/Caches/com.mycompany.appname/
回答by Chris Hawkins
My educated guess is that the memory use you are seeing is not from the page content, but rather from loading UIWebView and all of it's supporting WebKit libraries. I love the UIWebView control, but it is a 'heavy' control that pulls in a very large block of code.
我有根据的猜测是,您看到的内存使用不是来自页面内容,而是来自加载 UIWebView 和所有它支持的 WebKit 库。我喜欢 UIWebView 控件,但它是一个“重”控件,会引入非常大的代码块。
This code is a large sub-set of the iOS Safari browser, and likely initializes a large body of static structures.
此代码是 iOS Safari 浏览器的一个大型子集,可能会初始化大量静态结构体。
回答by Blasco73
After various attempt, only this works well for me (under ios 8):
经过各种尝试,只有这对我很有效(在 ios 8 下):
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:1 diskCapacity:1 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
回答by sebspi
For swift 2.0:
对于 swift 2.0:
let cacheSizeMemory = 4*1024*1024; // 4MB
let cacheSizeDisk = 32*1024*1024; // 32MB
let sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath: "nsurlcache")
NSURLCache.setSharedURLCache(sharedCache)
回答by Evgeni Petrov
I am loading html pages from Documents and if they have the same name of css file UIWebView
it seem it does not throw the previous css rules. Maybe because they have the same URL or something.
我正在从文档加载 html 页面,如果它们具有相同名称的 css 文件,UIWebView
它似乎不会抛出以前的 css 规则。也许是因为它们具有相同的 URL 或其他什么。
I tried this:
我试过这个:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
I tried this :
我试过这个:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
I am loading the initial page with:
我正在加载初始页面:
NSURLRequest *appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
It refuses to throw its cached data! Frustrating!
它拒绝抛出缓存的数据!令人沮丧!
I am doing this inside a PhoneGap (Cordova) application. I have not tried it in isolated UIWebView.
我在 PhoneGap (Cordova) 应用程序中执行此操作。我还没有在隔离的 UIWebView 中尝试过。
Update1:I have found this.
Changing the html files, though seems very messy.
更新 1:我找到了这个。
更改 html 文件,虽然看起来很乱。