2014-11-24 39 views
1

我需要刪除wkwebview中的緩存。我使用下面的代碼但沒有成功。在wkwebview中刪除緩存目標c

[[NSURLCache sharedURLCache]removeCachedResponseForRequest:request]; 
    [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
    [[NSURLCache sharedURLCache] setDiskCapacity:0]; 
    [[NSURLCache sharedURLCache] setMemoryCapacity:0]; 
+1

要刪除,請解釋更要做什麼。 – Ayaz 2014-11-24 12:54:12

+0

你怎麼知道它不工作? – 2014-11-24 12:59:53

+0

我在我的應用程序中進行了測試。我有不同的網頁。當我加載第一頁時,一些圖像被緩存,所有這些圖像都在第二頁加載。我嘗試了上面的代碼行來刪除緩存,但每次它正在從緩存中挑選圖像。加載第一頁花費了8秒,而在'wkwebview'中花了第二頁花了4秒。但是在uiwebview的第一頁花了10秒,第二頁花了8秒加載。在'uiwebview'我用過了以上代碼。 – Vandana 2014-11-26 17:53:19

回答

-1

嘗試在應用程序加載時首先設置緩存。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    int cacheSizeMemory = 4*1024*1024; // 4MB 
    int cacheSizeDisk = 32*1024*1024; // 32MB 
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; 
    [NSURLCache setSharedURLCache:sharedCache]; 

} 

一旦它正確配置,那麼當你需要清除緩存(例如,在applicationDidReceiveMemoryWarning或者當你關閉一個UIWebView)只要致電:

[[NSURLCache sharedURLCache] removeAllCachedResponses]; 

另一件事是,你可能想要嘗試清除cookieStorage。我知道這會重置您登錄的網站等。希望這可以幫助。

NSHTTPCookie *cookie; 
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
for (cookie in [storage cookies]) 
{ 
    [storage deleteCookie:cookie]; 
} 
+0

是的,這對於'uiwebview'工作正常,但對'wkwebview'不適用。 – Vandana 2014-11-26 17:48:24

0
NSSet *dataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache, 
             WKWebsiteDataTypeMemoryCache, 
             ]]; 
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:dataTypes 
              modifiedSince:[NSDate dateWithTimeIntervalSince1970:0] 
             completionHandler:^{ 
     }];