Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cookies for WKWebView?

For an iOS 8 app I want to use a WKWebView for a custom authentication ViewController that I'm building. However, I can't seem to figure out how to clear the stored cookies for the WKWebView. Is it not possible at all, right now?

I don't have control over the server side, and the service is sending what looks like a permanent (or at least a long lived) cookie when the user logs in successfully. The problem is, if the user wants to change their login, then it becomes impossible, because even if the user logs out and presses login again, then the server automatically redirects using the stored cookies and logs them back again.

Open to ideas and suggestions, thanks!

In UIWebView it was simple to clear stored cookies, all you had to do was this:

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

But, the WKWebView does not seem to use the NSHTTPCookieStorage because I've already tried to do this before loading the request in the WKWebView! :(

like image 349
Dhiraj Gupta Avatar asked Oct 01 '14 15:10

Dhiraj Gupta


People also ask

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.

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

What is IOS 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.


2 Answers

The answer was provided to me on the internal Apple forums: use a mutable NSURLRequest, and set HTTPShouldHandleCookies to NO:

let req: NSMutableURLRequest = NSMutableURLRequest(URL:openURL)
req.HTTPShouldHandleCookies = false
webView.loadRequest(req)

No cookies sent to the web site, so you get the login screen (for testing) every time.

like image 109
David H Avatar answered Oct 15 '22 15:10

David H


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 !

like image 37
Dhiraj Gupta Avatar answered Oct 15 '22 15:10

Dhiraj Gupta