-
-
Save insidegui/4a5de215a920885e0f36294d51263a15 to your computer and use it in GitHub Desktop.
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") | |
} | |
} | |
} | |
} |
Thank you, very much!
You saved my time for a wholeday, Thanks :)
👍 Just a note for others, Be sure to call this on the main thread!
You save my day! Thank you!
Thanks a lot. :)
Thanks a lot. Cheers :)
Objective C Implementation
[[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:WKWebsiteDataStore.allWebsiteDataTypes completionHandler:^(NSArray<WKWebsiteDataRecord *> * _Nonnull records) {
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:WKWebsiteDataStore.allWebsiteDataTypes forDataRecords:records completionHandler:^{
resolve(nil);
}];
}];
Thank you :)
Hi Guys,
I have used below code but facing issue. I have call clean function on log out but after reset password unable to receive cookies.
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")
}
}
}
Please help me asap.
Thank you
From a privacy point of view, I would recommend to not log the "record" object, just in case because it might leak sensitive information in your logs...
Definitely not. This code should not be in a production app, only in debug builds for testing. If you do ship this and want logs, use os_log
so that sensitive information gets redacted.
How to use for WKWebView ?
thank you
hey guys, is there a way to delete WKWebView's cache before a certain date? Like i want to only delete all the cache 15 days before.
many thanks
🙌
This unnecessarily loops through the records, and does not provide a completion handler for all clearing. The following is equivalent:
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: records, completionHandler: {
// All done!
})
}
perfect.
Many thanks
Thanks Brother
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")
}
}
Above code doesn't work on iOS15. Below code works well.
This unnecessarily loops through the records, and does not provide a completion handler for all clearing. The following is equivalent:
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast) WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: records, completionHandler: { // All done! }) }
Here's the different types of code using async/await
(Without the foreach. This works for me.)
let dataRecords = await WKWebsiteDataStore.default().dataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes())
await WKWebsiteDataStore.default().removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: dataRecords)
(With the foreach. Requires CollectionConcurrencyKit)
let dataRecords = await WKWebsiteDataStore.default().dataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes())
await dataRecords.asyncForEach { record in
await WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record])
}
Thank you very much
Thanks man!
Team,
Where we find all WebKit data in our IOS App storage ?
Very useful, thank you.