2012-08-22 68 views
10

如何清除/清空Cocoa應用程序中的WebView緩存?如何清除Cocoa中的WebView緩存?

特別是,我想清除本地樣式表的緩存。

我曾嘗試以下無濟於事:

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

// Also tried this before and after loadRequest 
[webView.mainFrame reloadFromOrigin]; 

即使有一個新的還是使用緩存的樣式表更換WebView

+0

看看這個討論http://stackoverflow.com/a/5606703/1578508 – lukaswelte

+0

@lukaswelte沒有在那裏工作。 – hpique

回答

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; 
} 

有趣的東西。

+0

是啊,太棒了,謝謝你寫下來 – aneuryzm

+0

這似乎並沒有爲我工作。 OSX 10.10.3。 – mattsven

+0

這在10.12不適用於我。到目前爲止,我發現的唯一解決方案是嵌入本地樣式表,而不是鏈接到它。 – Brett

0

看看NSURLCache Class Reference.

- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path 
//set memoryCapacity and diskCapacity to 0 bytes and diskPath to nil 

NSURLCache *Cache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; 
[NSURLCache setSharedURLCache:Cache]; 
+0

有趣,但似乎沒有工作。本地樣式表的緩存仍然被加載。 – hpique