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?
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.
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.
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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With