How to remove cache in WKWebview?

IosIos8Wkwebview

Ios Problem Overview


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

P.S. : Normal NSURLCache is not working.

Ios Solutions


Solution 1 - Ios

Updated Swift 5 version:

WKWebsiteDataStore.default().removeData(ofTypes: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache], modifiedSince: Date(timeIntervalSince1970: 0), completionHandler:{ })

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()
}

Swift 3 version:

   if #available(iOS 9.0, *)
    {
        let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
        let date = NSDate(timeIntervalSince1970: 0)
        
        WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{ })
    }
    else
    {
        var libraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, false).first!
        libraryPath += "/Cookies"
        
        do {
            try FileManager.default.removeItem(atPath: libraryPath)
        } catch {
            print("error")
        }
        URLCache.shared.removeAllCachedResponses()
    }

Solution 2 - Ios

In iOS 9

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

Solution 3 - Ios

Remove Cache and Cookies

# WKWebView+Clean.swift

import WebKit

extension WKWebView {
    class func clean() {
        guard #available(iOS 9.0, *) else {return}

        HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)

        WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
            records.forEach { record in
                WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
                #if DEBUG
                    print("WKWebsiteDataStore record deleted:", record)
                #endif
            }
        }  
    }
}

Example:

import WebKit
...
WKWebview.clean()

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

Something similar for iOS 9.0 and later:

let store: WKWebsiteDataStore = WKWebsiteDataStore.default()
let dataTypes: Set<String> = WKWebsiteDataStore.allWebsiteDataTypes()
store.fetchDataRecords(ofTypes: dataTypes, completionHandler: { (records: [WKWebsiteDataRecord]) in
  store.removeData(ofTypes: dataTypes, for: records, completionHandler: {
    // do your thing here
  })
})

Solution 7 - Ios

Worked
Swift 3. Version.
For Older Version Swift

import Foundation
import WebKit

func removeWebData() {
    
    if #available(iOS 9.0, *) {
        
        let websiteDataTypes = WKWebsiteDataStore.allWebsiteDataTypes()
        let date = NSDate(timeIntervalSince1970: 0)
        
        WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes, modifiedSince: date as Date, completionHandler: {
            #if DEBUG
                
                print("remove all data in iOS9 later")
                
            #endif
        })
        
    }else {
        
        // Remove the basic cache.
        URLCache.shared.removeAllCachedResponses()
        
        // Delete system cookie store in the app
        let storage = HTTPCookieStorage.shared
        if let cookies = storage.cookies {
            for cookie in cookies {
                storage.deleteCookie(cookie)
            }
        }
        
        
        do {
            // folder 를 삭제하는 대신 contents 를 삭제하는 이유?
            // MainVC 가 호출되면 cache, cookie가 발생하므로 로딩시 확인된 cache, cookie 폴더의
            // contents 에 대해서만 삭제 진행.
            
            // Clear web cache
            try deleteLibraryFolderContents(folder: "Caches")
            
            // Remove all cookies stored by the site. This includes localStorage, sessionStorage, and WebSQL/IndexedDB.
            try deleteLibraryFolderContents(folder: "Cookies")
            
            // Removes all app cache storage.
            try deleteLibraryFolder(folder: "WebKit")
            
        } catch {
            #if DEBUG
                
                print("Delete library folders error in iOS9 below")
                
            #endif
            
        }
    }
}

/**
 Delete folder in library
 
 - parameter folder: a folder you want to delete
 
 - throws: throws an error
 */
func deleteLibraryFolder(folder: String) throws {
    let manager = FileManager.default
    let library = manager.urls(for: .libraryDirectory, in: .userDomainMask).first!
    let dir = library.appendingPathComponent(folder)
    try manager.removeItem(at: dir)
}

/**
 Delete contents in library folder
 
 - parameter folder: target folder
 
 - throws: throws an error
 */
private func deleteLibraryFolderContents(folder: String) throws {
    let manager = FileManager.default
    let library = manager.urls(for: FileManager.SearchPathDirectory.libraryDirectory, in: .userDomainMask)[0]
    let dir = library.appendingPathComponent(folder)
    let contents = try manager.contentsOfDirectory(atPath: dir.path)
    for content in contents {
        do {
            try manager.removeItem(at: dir.appendingPathComponent(content))
        } catch where ((error as NSError).userInfo[NSUnderlyingErrorKey] as? NSError)?.code == Int(EPERM) {
            // "EPERM: operation is not permitted". We ignore this.
            #if DEBUG
                
                print("Couldn't delete some library contents.")
                
            #endif
        }
    }
}

Solution 8 - Ios

private func deleteCache(){
        if #available(iOS 9.0, *) {
          let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
          let date = NSDate(timeIntervalSince1970: 0)
            WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{ })
        } else {
            var libraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, false).first!
            libraryPath += "/Cookies"

            do {
                try FileManager.default.removeItem(atPath: libraryPath)
            } catch {
              print("error")
            }
            URLCache.shared.removeAllCachedResponses()
        }
    }

Solution 9 - Ios

swift5

URLCache.shared.removeAllCachedResponses()

Solution 10 - Ios

This one-liner should do the trick by removing all WKWebView data.

WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), modifiedSince: Date(timeIntervalSince1970: 0), completionHandler: {})

Solution 11 - Ios

It works for me

 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
        {
          NSURLCache.sharedURLCache().removeAllCachedResponses()
        }

Solution 12 - Ios

Swift 3.x version of @Esqarrouth's post

func clearCache() {
    if #available(iOS 9.0, *) {
        let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
        let date = NSDate(timeIntervalSince1970: 0)
        WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{ })
    } else {
        var libraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, false).first!
        libraryPath += "/Cookies"
        
        do {
            try FileManager.default.removeItem(atPath: libraryPath)
        } catch {
            print("error")
        }
        URLCache.shared.removeAllCachedResponses()
    }
}

Solution 13 - Ios

It takes me a few hours, but this works for me! After the loadRequest, I make a reloadFromOrigin! So I force empty cache! [WKWebView loadRequest:request]; [WKWebView reloadFromOrigin];

Solution 14 - Ios

get the cache dictionary path and delete it

   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSError *error;
  BOOL success = [fileManager removeItemAtPath:documentsPath error:&error];
    NSLog(@"%d",success);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSravanView Question on Stackoverflow
Solution 1 - IosEsqarrouthView Answer on Stackoverflow
Solution 2 - IosShingo FukuyamaView Answer on Stackoverflow
Solution 3 - IosPeter KreinzView Answer on Stackoverflow
Solution 4 - IosharrisonleeView Answer on Stackoverflow
Solution 5 - IosShingo FukuyamaView Answer on Stackoverflow
Solution 6 - IosnouatziView Answer on Stackoverflow
Solution 7 - IosZAFAR007View Answer on Stackoverflow
Solution 8 - IosKang DeveloperView Answer on Stackoverflow
Solution 9 - IosKseniyaView Answer on Stackoverflow
Solution 10 - IosatulkhatriView Answer on Stackoverflow
Solution 11 - IosDurai Amuthan.HView Answer on Stackoverflow
Solution 12 - Ioszingle-dingleView Answer on Stackoverflow
Solution 13 - IosValentin ThomallaView Answer on Stackoverflow
Solution 14 - IosSateesh PasalaView Answer on Stackoverflow