如何在Cocoa中清除WebView缓存?

hpi*_*que 10 macos cocoa webview

如何在Cocoa应用程序中清除/刷新WebView缓存?

特别是,我想清除本地样式表的缓存.

我试过以下无济于事:

// Tried this before loadRequest
[[NSURLCache sharedURLCache] removeAllCachedResponses];

// Also tried this before and after loadRequest
[webView.mainFrame reloadFromOrigin];
Run Code Online (Sandbox Code Playgroud)

WebView使用新的替换它仍然使用缓存的样式表.

hpi*_*que 10

其他建议的解决方案不适用于本地样式表(尽管它们应该适用于远程资源).

我终于通过显式设置缓存策略,通过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;
}
Run Code Online (Sandbox Code Playgroud)

好玩的东西.