Clear cookies for WKWebView?

后端 未结 2 1764
予麋鹿
予麋鹿 2020-12-18 01:06

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

相关标签:
2条回答
  • 2020-12-18 01:34

    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 !

    0 讨论(0)
  • 2020-12-18 01:35

    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.

    0 讨论(0)
提交回复
热议问题