1
votes

How to delete WKWebview 's Cache in iOS8? For iOS 9 Below code was working.

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

For iOS 8, I tried solutions in following links, but Cache is not deleted.

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/

I would appreciate your help.

1

1 Answers

2
votes

I am working on browser for iOS and wanna to share our experience in that question.

First of all, all webviews in our browser connected between each other via single processPool. It leads to sharing cookies between all webViews. To do this, we set same processPool to WKWebViewConfiguration, that is passed to newly created webView:

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

Secondly, the process of data removing looks like this:

  1. Remove all created webViews

  2. Remove directories with cache/cookies

  3. Create new process pool

  4. Recreate webViews with new process pool

If you have 1 webView, the whole process should look like this:

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

I want to draw your attention, that this code works only on device in iOS 8.x. It doesn't work in simulator at all.