如何清除/刷新 WebView在 Cocoa 应用程序中缓存?
特别是,我想清除本地样式表的缓存。
我尝试了以下方法,但没有成功:
// Tried this before loadRequest
[[NSURLCache sharedURLCache] removeAllCachedResponses];
// Also tried this before and after loadRequest
[webView.mainFrame reloadFromOrigin];
即使将 WebView
替换为新的,仍然使用缓存的样式表。
最佳答案
其他建议的解决方案不适用于本地样式表(尽管它们应该适用于远程资源)。
我终于设法通过 resourceLoadDelegate 解决了这个问题,通过显式设置缓存策略:
- (NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource {
request = [NSURLRequest requestWithURL:[request URL] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:[request timeoutInterval]];
return request;
}
有趣的东西。
关于macos - 如何清除 Cocoa 中的 WebView 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072076/