Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete WKWebview cookies

For now I am doing like this

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

But it is not working on iOS 8, 64-bit device.

Any other way the clean cookies of WKWebview? Any help will be appreciated. thanks.

like image 767
Pankaj Gaikar Avatar asked Jul 08 '15 10:07

Pankaj Gaikar


People also ask

Does WKWebView store cookies?

The code above has no effect with the new web view since each WKWebView instance has its own cookie storage represented by WKHTTPCookieStore class.

How do I reset WKWebView?

To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .

Does WKWebView share cookies with Safari?

WKWebView is an in-app browser that displays web content. It doesn't share cookies or web site data with other WKWebView instances, or with the Safari browser.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


1 Answers

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:

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);              }          ];        }      }    }  ]; 

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

Below is 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   ) } 
like image 65
Pankaj Gaikar Avatar answered Sep 18 '22 09:09

Pankaj Gaikar