Monday, January 15, 2018

Deleting cookies with WKHTTPCookieStore

Leave a Comment

I'm using the new WKHTTPCookieStore class in order to inject and delete cookies from WKWebViews in an app.

All of the WKWebViews share a common WKWebViewConfiguration so that they can share a common cookie store.

Injecting cookies works fine using the add() method, and each of the web views can see the new cookies and send them with their requests. Deleting cookies seems to be a problem - the web views all still see the supposedly deleted cookie, and continue to send it with each request:

let cookieStore = self.webkitConfiguration.websiteDataStore.httpCookieStore cookieStore.getAllCookies { (cookies) in     for cookie:HTTPCookie in cookies {         if cookie.name == "CookieIWantToDelete" {             cookieStore.delete(cookie, completionHandler: {                 self.webView.reload() //Deleted cookie is still sent with this request             })         }     } } 

I can work around it by trashing all of the cookies in the WKWebsiteDataStore, but it seems a bit overkill.

Any ideas?

1 Answers

Answers 1

You need to clear WKWebView cache before reloading by using URLCache.shared.removeAllCachedResponses(), for exampe, or use self.webView.reloadFromOrigin() to load fresh data.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment