Skip to content

Instantly share code, notes, and snippets.

@zsmatrix62
Forked from insidegui/WebCacheCleaner.swift
Created August 17, 2021 14:13
Show Gist options
  • Save zsmatrix62/88b036a3ec8668a3c0d0df7c72522a7e to your computer and use it in GitHub Desktop.
Save zsmatrix62/88b036a3ec8668a3c0d0df7c72522a7e to your computer and use it in GitHub Desktop.
Clear WKWebView's cookies and website data storage, very useful during development.
import Foundation
import WebKit
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")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment