如何在iOS8中删除WKWebview的缓存?

时间:2016-07-20 12:07:40

标签: ios swift caching cookies wkwebview

如何在iOS8中删除WKWebview的缓存? 对于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,我在以下链接中尝试了解决方案,但未删除缓存。

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

How to remove cache in WKWebview?

remove cache in wkwebview objective c

How to delete WKWebview cookies

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

http://atmarkplant.com/ios-wkwebview-tips/

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我正在开发适用于iOS的浏览器,并想分享我们在该问题上的经验。

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

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

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

  1. 删除所有已创建的网络视图

  2. 删除包含缓存/ cookie的目录

  3. 创建新流程池

  4. 使用新流程池

  5. 重新创建webViews

    如果您有1个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 中的设备上。它根本不适用于模拟器。