【发布时间】:2014-06-13 14:38:52
【问题描述】:
我正在开发一个应用程序,其中很少从服务器下载 HTML、CSS 文件并像这样加载到 webview 中
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURL *url = [NSURL fileURLWithPath:htmlfile];
htmlLoadRequest = [NSURLRequest requestWithURL:url];
[self.objwebview loadRequest:htmlLoadRequest];
在 webview 委托方法中我有这段代码
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
if(request.cachePolicy != NSURLRequestReloadIgnoringLocalCacheData) {
NSURLRequest* noCacheRequest = [[NSURLRequest alloc] initWithURL:request.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:request.timeoutInterval];
[webView loadRequest:noCacheRequest];
} // further code to do other stuff
在 viewWillDisappear 方法中我有这段代码
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.objwebview stopLoading];
[self.objwebview setDelegate:nil];
if (htmlLoadRequest) {
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:htmlLoadRequest];
}
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSString *htmlfile = [HTML_SERVER_FILES stringByAppendingPathComponent:@"CoverPage.html"];
self.objwebview = nil;
}
我面临问题的地方是,当我第一次下载文件时,一切正常,但是当我更新我的几个 html 文件然后从服务器下载它们时,在这种情况下,我仍然会发生什么查看第一个 html 文件及其图像。
这意味着 webview 的某个地方仍在维护已下载的第一组缓存信息并且没有清除它。
另外,当我的视图控制器加载到视图中时,会出现我调用此代码的方法
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
但什么也没发生,我仍然看到最初从服务器下载的相同的旧 HTML 文件。
查看更新集的唯一方法是杀死应用程序,然后再次启动它,我发现不应该这样做,因为它会创建一个错误的用户 exp。所以请帮我解决这个问题。
【问题讨论】:
标签: objective-c webview uiwebview uiwebviewdelegate nsurlcache