Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting cookies with WKHTTPCookieStore

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?

like image 931
Nathan Gaskin Avatar asked Jan 05 '18 12:01

Nathan Gaskin


People also ask

Why can't I delete my cookies?

Moreover, if you try to remove the Google services cookies, the browser will automatically re-create them, making it impossible to remove data stored on Google servers. Thankfully, you can fix it by logging out of the Google services and then clearing out the browser history.

What is WKWebView?

Overview. 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.

How do I reset WKWebView?

To clear old contents of webview When you call - loadHTMLString:baseURL: it doesn't block until the load is complete. Once it records your request, it returns and loads in the background. As a result, you would need to wait for the first load to finish before kicking off a new load request.


1 Answers

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

like image 144
HereTrix Avatar answered Sep 22 '22 19:09

HereTrix