Xamarin iOS clear cache from WKWebView
From this question looks like you do this (c# version) for iOS 9 and it will print out which records are deleted:
var websiteDataTypes = new NSSet<NSString>(new []
{
//Choose which ones you want to remove
WKWebsiteDataType.Cookies,
WKWebsiteDataType.DiskCache,
WKWebsiteDataType.IndexedDBDatabases,
WKWebsiteDataType.LocalStorage,
WKWebsiteDataType.MemoryCache,
WKWebsiteDataType.OfflineWebApplicationCache,
WKWebsiteDataType.SessionStorage,
WKWebsiteDataType.WebSQLDatabases
});
WKWebsiteDataStore.DefaultDataStore.FetchDataRecordsOfTypes (websiteDataTypes, (NSArray records) =>
{
for (nuint i = 0; i < records.Count; i++) {
var record = records.GetItem<WKWebsiteDataRecord> (i);
WKWebsiteDataStore.DefaultDataStore.RemoveDataOfTypes (record.DataTypes,
new[] {record}, () => {Console.Write($"deleted: {record.DisplayName}");});
}
});
Or for iOS 8, from ShingoFukuyama/WKWebViewTips you could check the subdirectories Cookies, Caches, WebKit in the Library directory are removed.
iOS 8
After much trial and error, I've reached the following conclusion:
Use NSURLCache and NSHTTPCookie to delete cookies and caches in the same way as you used to do on UIWebView.
If you use WKProccessPool, re-initialize it.
Delete Cookies, Caches, WebKit subdirectories in the Library directory.
Delete all WKWebViews
How to delete WKWebview cookies
Apple released new APIs for iOS 9, so now we can remove domain specific cookies stored for WKWebView with below code, but this will only work on devices with iOS version 9 or later:
Below is the Swift 3 version
let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { (records) in
for record in records {
if record.displayName.contains("facebook") {
dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: [record], completionHandler: {
print("Deleted: " + record.displayName);
})
}
}
}
And Swift 4:
let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
dataStore.removeData(
ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
for: records.filter { $0.displayName.contains("facebook") },
completionHandler: completion
)
}
Objective-C version -
WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
[dateStore
fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
for (WKWebsiteDataRecord *record in records) {
if ( [record.displayName containsString:@"facebook"]) {
[[WKWebsiteDataStore defaultDataStore]
removeDataOfTypes:record.dataTypes
forDataRecords:@[record]
completionHandler:^{
NSLog(@"Cookies for %@ deleted successfully",record.displayName);
}
];
}
}
}
];
The above snippet will sure work for iOS 9 and later. Unfortunately, if we use WKWebView for iOS versions before iOS 9, we still have to stick to the traditional method and delete the whole cookies storage as below.
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
Related Topics
Error: Ld: Library Not Found for -Lpods with Cocoapods
Open Mobile Safari from a Link in a Webview
Swift 2.0 Sorting Array of Objects by Property
403 Error - Thats an Error. Error: Disallowed_Useragent
The Operation Couldn't Be Completed. (Com.Facebook.Sdk Error 2.) iOS6
Copy Nsattributedstring in Uipasteboard
Unwind Segue from Navigation Back Button in Swift
Does This App Use the Advertising Identifier (IDFA)? - Admob 6.8.0
Decimal to Fraction Conversion in Swift
Detect iOS Application About to Delete
Swift:How to Change Language Inside App
iOS Enterprise Ota Distribution Unable to Download Application
How to Resize an Image or Done as a Nsattributedstring Nstextattachment (Or Set Its Initital Size)
How to Add Different Types Conforming to a Protocol with an Associated Type to a Collection
Swiftui - iOS 13 Uiviewrepresentable of Wkwebview Gets Thread 1: Exc_Breakpoint Crash
Swift Get String Between 2 Strings in a String
Pixel Array to Uiimage in Swift
How to Create a Hex Color String Uicolor Initializer in Swift