如何清除缓存wkwebview?

(文章转载自:http://91r.net/ask/27105094.html)

如何清除缓存wkwebview?

 

 

Any one worked with WKWebview and tried to clear cache ? If yes, how to do? any example ?

P.S. : Normal NSURLCache is not working.

 

2015年12月22日24分51秒

 

In iOS 9

//// Optional data
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
                        WKWebsiteDataTypeDiskCache,
                        //WKWebsiteDataTypeOfflineWebApplicationCache,
                        WKWebsiteDataTypeMemoryCache,
                        //WKWebsiteDataTypeLocalStorage,
                        //WKWebsiteDataTypeCookies,
                        //WKWebsiteDataTypeSessionStorage,
                        //WKWebsiteDataTypeIndexedDBDatabases,
                        //WKWebsiteDataTypeWebSQLDatabases
                        ]];
//// All kinds of data
//NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
//// Date from
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
//// Execute
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
   // Done
}];

 

2015年12月22日24分51秒

 

You cannot delete caches only with NSURLCache. After much cut-and-try, I could clear caches by the following steps (as of iOS 8.1.1):

  1. Use NSURLCache to delete caches in the same way as you used to do on UIWebView. If you use WKProccessPool, re-initialize it.
  2. Delete Caches directory in Library.
  3. Delete all WKWebViews

Next time you create WKWebView, caches should be cleared.

ShingoFukuyama/WKWebViewTips

 

2015年12月22日24分51秒

 

I was able to solve this issue by having my WKWebView evaluate window.location.reload. I have a tap gesture recognizer on the web view and whenever it detects a double tap with 2 touches, I run the following:

webView.evaluateJavaScript("window.location.reload(true)", completionHandler: nil);

Hope this helps.

 

2015年12月22日24分51秒

 

Swift version:

if #available(iOS 9.0, *) {
  let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
  let date = NSDate(timeIntervalSince1970: 0)
  WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })
} else {
    var libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, false).first!
    libraryPath += "/Cookies"

    do {
      try NSFileManager.defaultManager().removeItemAtPath(libraryPath)
    } catch {
      print("error")
    }
    NSURLCache.sharedURLCache().removeAllCachedResponses()
}

 

2015年12月22日24分51秒

 

 

posted @ 2015-12-23 15:55  我叫南部21号  阅读(1073)  评论(0)    收藏  举报