How to Delete Wkwebview Cookies

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 * __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];

How to delete cookies from WKHttpCookieStore?

Update: Previously I filled a bugreport at bugreport.apple.com and now I can confirm this is fixed in iOS 12.


I jumped to conclusion that it's an iOS bug. So, instead of deleting cookies now I'm replacing them with cookies with an empty value:

NSDictionary properties = NSDictionary.FromObjectsAndKeys(
objects: new NSObject[]
{
new NSString(cookieToDelete.Name ?? ""),
new NSString(""),
new NSString(cookieToDelete.Path ?? ""),
new NSString(cookieToDelete.Domain ?? ""),
},
keys: new NSObject[]
{
NSHttpCookie.KeyName,
NSHttpCookie.KeyValue,
NSHttpCookie.KeyPath,
NSHttpCookie.KeyDomain,
}
);
NSHttpCookie cookieToReplace = NSHttpCookie.CookieFromProperties(properties);
await cookieStore.SetCookieAsync(cookieToReplace);

Clear cookies for WKWebView?

It seems like NSHTTPCookieStorage is now being used in iOS 8.2 to correctly clear cookies, as required. I had shipped an app which would run this code prior to opening a WKWebView based login:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}

Where earlier than iOS 8.2 the website would auto-login using the saved cookies, it now correctly asks the user to re-login. All this happened without me shipping an update to the app. :)

Thanks for the heads-up @jackreichert !

Clear webView cookies (Swift)

I made it by

        let cookieJar = NSHTTPCookieStorage.sharedHTTPCookieStorage()

for cookie in cookieJar.cookies! {
// print(cookie.name+"="+cookie.value)
cookieJar.deleteCookie(cookie)
}

Swift 4

func removeCookies(){
let cookieJar = HTTPCookieStorage.shared

for cookie in cookieJar.cookies! {
cookieJar.deleteCookie(cookie)
}
}

Fail to clear cookies without a WKWebView alive

Just use another WKProcessPool after clear cookies, and no cache any more.



Related Topics



Leave a reply



Submit