【发布时间】:2011-02-01 04:18:46
【问题描述】:
我需要 UIWebView 来显示一些本地 .webarchive 文件。但是那里的图像名称相同,因此 UIWebView 始终只显示一张图像。如何清除缓存?
提前致谢
【问题讨论】:
我需要 UIWebView 来显示一些本地 .webarchive 文件。但是那里的图像名称相同,因此 UIWebView 始终只显示一张图像。如何清除缓存?
提前致谢
【问题讨论】:
// Flush all cached data
[[NSURLCache sharedURLCache] removeAllCachedResponses];
【讨论】:
link 标签中包含的样式表)。我在UIWebView 上使用-loadHTMLString:baseURL: 方法。安装一个 NSURLCache 子类就可以了,但我不会在调试场景之外使用它。
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[webView loadRequest:request];
(现在修正了错字)
【讨论】:
我自己也遇到过缓存脚本和 css 文件的问题。
我的解决方案是在 src url 上放置一个缓存清除参数:
<script src='myurl?t=1234'> ...
其中 t=1234 每次页面加载时都会发生变化。
【讨论】:
使用
[[NSURLCache sharedURLCache] removeAllCachedResponses];
在调用之前或加载视图控制器时,这是一个静态函数,用于删除响应的所有缓存数据。 仅使用 cachePolicy 是不够的,我看到 uiwebview 可以刷新 html 文档,但不能刷新 CSS、JS 或图像等链接文档。
我尝试了这个解决方案并且它有效。
if (lastReq){
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:lastReq];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
lastReq=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:localUrl]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10000];
[lastReq setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[self.mainWebView loadRequest:lastReq];
--首先,我删除最后一个请求的缓存并调用删除所有其他缓存(我认为这不是那么重要) --然后我创建一个 nsurlrequest 并忽略LocalCacheData --最后,加载新的请求
我检查它并重新加载所有文件(html、css、js 和图像)。
【讨论】:
//to prevent internal caching of webpages in application
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
sharedCache = nil;
尝试使用它。它将清除应用程序的 url 缓存。
【讨论】:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
removeAllCachedResponses 什么也没做。
我正在与一些设计师合作,编辑应用中某些 Web 视图使用的 CSS 文件。他们抱怨样式表的更改没有反映在应用程序中,并且使用Charles 进行的一些调试确认它们没有被重新加载。我似乎在 StackOverflow 上尝试了所有答案,但无济于事。
最后的诀窍是创建一个覆盖-cachedResponseForRequest: 的NSURLCache 子类
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
{
if ([[[[request URL] absoluteString] pathExtension] caseInsensitiveCompare:@"css"] == NSOrderedSame)
return nil;
else
return [super cachedResponseForRequest:request];
}
然后我用合理的内存和磁盘容量安装它:
NSURLCache *currentCache = [NSURLCache sharedURLCache];
NSString *cachePath = [cachesDirectory() stringByAppendingPathComponent:@"DebugCache"];
DebugURLCache *cache = [[DebugURLCache alloc] initWithMemoryCapacity:currentCache.memoryCapacity diskCapacity:currentCache.diskCapacity diskPath:cachePath];
[NSURLCache setSharedURLCache:cache];
[cache release];
(cachesDirectory()是一个函数,返回iOS应用目录下的/Library/Caches)。
正如您所见,我仅在调试配置中使用它(使用 Additional Preprocessor Flags 构建设置和一些 #ifdefs)。
【讨论】:
对于 swift 3
//to remove cache from UIWebview
URLCache.shared.removeAllCachedResponses()
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
【讨论】:
使用以下代码:
NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[[NSFileManager defaultManager]removeItemAtPath:cacheDir error:nil];
【讨论】:
这项工作就像一个魅力!
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//Clear All Cookies
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
【讨论】: