无法清除UIWebView缓存

时间:2014-06-07 06:42:45

标签: ios objective-c xcode caching uiwebview

我在iOS应用程序中使用UIWebView,它不会停止加载旧资产。我的index.html引用了多个javascript文件似乎没有更新,除非我清理构建文件夹,即便如此,它们有时仍会存在。我可以从项目中删除带有html,css和javascript的文件夹,将其删除,然后在Web视图中显示并运行它。

我的AppDelegate使用

清除缓存
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
在'didFinishLaunchingWithOptions'之后。然后我将文件添加到Web视图中:

NSURL *mainBundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[_webView loadHTMLString:htmlString baseURL:mainBundleURL];

我也尝试使用请求加载文件,但我得到了相同的结果。

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:60.0];
[self.webView loadRequest:request];

我尝试将该文件夹添加为项目中的文件夹引用和组引用。该文件夹可以在Copy Bundle Resources中找到。我已经开始创建一个新项目,但问题仍然存在。

这似乎是一个简单的问题,但我已经尝试了一些我能在网上找到的东西。

1 个答案:

答案 0 :(得分:1)

试试这可能会有所帮助......

 NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];  
 [NSURLCache setSharedURLCache:sharedCache];
 [sharedCache release];

谢谢&欢呼声