【发布时间】:2014-02-18 20:54:27
【问题描述】:
我可以通过以下方式创建缓存条目:
[[NSURLCache sharedURLCache] storeCachedResponse:cachedURLresponse forRequest:request];
对于网址http://www.punkoffice.com/hello.html
如果我在看到我的缓存条目存在后直接运行它
NSLog(@"cache = %@",[[NSURLCache sharedURLCache] cachedResponseForRequest:request]);
我可以在 SQLite 数据库文件中看到它。
但是,当我使用完全相同的请求执行加载请求时:
[self.webView loadRequest:request]
未找到该请求的缓存条目,并创建了一个新条目。它最终会覆盖我之前放入的缓存条目。
如何获取 loadRequest 以查看我使用 storeCachedResponse 存储的缓存条目?
(编辑) 这是我用来创建缓存条目的完整代码。我可以告诉这个位有效,因为它存在于缓存数据库中,我可以从 cachedReponseForRequest 中检索它
NSString *strURL = @"http://www.punkoffice.com/hello.html";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSString *strData = @"<html><body>Some other content</body></html>";
NSData *dataContent = [strData dataUsingEncoding:NSUTF8StringEncoding];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[NSURL URLWithString:strURL] MIMEType:@"text/html" expectedContentLength:[dataContent length] textEncodingName:nil];
NSCachedURLResponse *cachedURLresponse = [[NSCachedURLResponse alloc] initWithResponse:response data:dataContent];
我也尝试过像这样添加标题:
NSDictionary *headers = @{@"Cache-Control":@"max-age=3600"};
NSHTTPURLResponse* httpResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:strURL] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:headers];
NSCachedURLResponse *cachedURLresponse = [[NSCachedURLResponse alloc] initWithResponse:httpResponse data:dataContent];
【问题讨论】:
标签: ios objective-c caching uiwebview