Created
September 14, 2016 23:12
-
-
Save insidegui/4a5de215a920885e0f36294d51263a15 to your computer and use it in GitHub Desktop.
Clear WKWebView's cookies and website data storage, very useful during development.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} | |
} | |
} |
🙌
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 ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
many thanks