1

I used the following code to clear cookies and site data and it works well

final class WebCacheCleaner {

    class func clean() {
        HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
        print("[WebCacheCleaner] All cookies deleted")
        WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
            records.forEach { record in
                WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
                print("[WebCacheCleaner] Record \(record) deleted")
            }
        }
    }
}

But what should I do to retrieve cookies and site data? I want to save again after restoring them.

koen
  • 5,383
  • 7
  • 50
  • 89
J56
  • 37
  • 5
  • do you check this answer?: [cookies](https://stackoverflow.com/a/47828324/5909442) – Andres Gomez Dec 31 '19 at 17:01
  • It only restores the cookie but I want the data of the sites that have been deleted as well @AndresGomez – J56 Dec 31 '19 at 17:05
  • Can you save the records outside of `WKWebsiteDataStore` and restore them when needed? – koen Dec 31 '19 at 17:35
  • i didnt understand Koen . Could you explain more or give me a code? @koen – J56 Dec 31 '19 at 18:00
  • Maybe by using the `FileManager` API to store your records locally. – koen Dec 31 '19 at 18:32
  • So how to store records? . i dont know how to do this @koen – J56 Jan 01 '20 at 07:51
  • Looks your snippet comes from this page: https://stackoverflow.com/questions/27105094/how-to-remove-cache-in-wkwebview. There are also some snippets about using the FileManager, although it is about removing data. That should give you a start. – koen Jan 01 '20 at 17:28

0 Answers0