Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

authentication - 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! :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...