如何删除iOS8中的WKWebview的caching?

如何删除iOS8中的WKWebview的caching? 对于iOS 9下面的代码工作。

let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeCookies]) let date = NSDate(timeIntervalSince1970: 0) WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ }) 

对于iOS 8,我尝试了以下链接的解决scheme,但不会删除caching。

https://github.com/ShingoFukuyama/WKWebViewTips#cookie-cache-credential-webkit-data-cannot-easily-delete

如何删除WKWebview中的caching?

在wkwebview目标c中删除caching

如何删除WKWebview cookies

http://blogs.candoerz.com/question/128462/how-to-delete-wkwebview-cookies.aspx

iOS WKWebView Tips

我将不胜感激您的帮助。

我正在为iOS浏览器工作,并希望分享我们在这个问题上的经验。

首先,我们浏览器中的所有网页浏览通过单个processPool相互连接。 它导致在所有webView之间共享cookie。 为此,我们将相同的processPool设置为WKWebViewConfiguration,传递给新创build的webView:

  WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init]; configuration.processPool = self.processPool; WKWebView* webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration]; 

其次,数据删除的过程如下所示:

  1. 删除所有创build的webViews

  2. 使用caching/ Cookie删除目录

  3. 创build新的进程池

  4. 用新的进程池重新创buildwebViews

如果你有一个webView,整个过程应该是这样的:

 - (void)clearWebViewData { [self.webView removeFromSuperview]; self.webView = nil; NSFileManager* fileManager = [NSFileManager defaultManager]; NSURL* libraryURL = [fileManager URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:NULL create:NO error:NULL]; NSURL* cookiesURL = [libraryURL URLByAppendingPathComponent:@"Cookies" isDirectory:YES]; [fileManager removeItemAtURL:cookiesURL error:nil]; NSURL* webKitDataURL = [libraryURL URLByAppendingPathComponent:@"WebKit" isDirectory:YES]; NSURL* websiteDataURL = [webKitDataURL URLByAppendingPathComponent:@"WebsiteData" isDirectory:YES]; NSURL* localStorageURL = [websiteDataURL URLByAppendingPathComponent:@"LocalStorage" isDirectory:YES]; NSURL* webSQLStorageURL = [websiteDataURL URLByAppendingPathComponent:@"WebSQL" isDirectory:YES]; NSURL* indexedDBStorageURL = [websiteDataURL URLByAppendingPathComponent:@"IndexedDB" isDirectory:YES]; NSURL* mediaKeyStorageURL = [websiteDataURL URLByAppendingPathComponent:@"MediaKeys" isDirectory:YES]; [fileManager removeItemAtURL:localStorageURL error:nil]; [fileManager removeItemAtURL:webSQLStorageURL error:nil]; [fileManager removeItemAtURL:indexedDBStorageURL error:nil]; [fileManager removeItemAtURL:mediaKeyStorageURL error:nil]; WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init]; configuration.processPool = [[WKProcessPool alloc] init]; self.webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration]; [self.webView loadRequest:request]; } 

我想引起您的注意,这个代码只适用于iOS 8.x的设备 。 它根本不能在模拟器中工作。