WKWebView, get all cookies

4.1k views Asked by At

I want obtain all cookies from WKWebView. Why? I have been started a project that use web-based auth. As result, I should intercept cookies to be sure that user is logged in and for some other purposes. Another case - imagine if user logged in, and than he "kill" the app - due to some delay in storing this cookie session will be lost :(.

The problem seems to be that the cookies are cached and not saved out to a file immediately.

(@Kemenaran from here - p.5 below)

The point where I try to catch them -

webView:decidePolicyForNavigationResponse:decisionHandler:,

func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {

  if let httpResponse = navigationResponse.response as? NSHTTPURLResponse {
    if let headers = httpResponse.allHeaderFields as? [String: String], url = httpResponse.URL {
      let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headers, forURL: url {
      for cookie in cookies {
            NSHTTPCookieStorage.shared.set(cookie)
      }
    }
  }
}

but not all request are navigation, so one cookie (in my case) is skipped, see details below

enter image description here

Few words about other option I tried...

  1. Yes, i Know that starting from iOS 11, we can use WKHTTPCookieStore as mention here. But my project should support iOS 9+

enter image description here

  1. I for 100% sure, that after 5-10 sec from login, required cookie will be saved to NSHttpCookieStorage (at least all my tests during few days confirm that)

  2. I try to use provided observer NSHTTPCookieManagerCookiesChangedNotification, but it provide me callback only for cookies that comes within webView:decidePolicyForNavigationResponse:decisionHandler

  3. I also try to get cookies using some JS like mentioned here and also test all suggestion from here - really great article by the way. Result - negative

  4. I also found this radar bug, and this SO question, and Sample project, but I want to prevent even this case. (described in this post applicable not only for remove but and for save) Also this situation true and when user kill the app, so case when user login, kill app and relaunch, may be present. And preventing this (simply by checking NSHttpCookieStorage for required cookies are also not good idea, because exactly after login required cookie can be stored with some delay, so this approach requires some bool-powered solution, that looks like weird..

  5. I also read few more SO post for some related problem, and the most usefull are

But still without good solution...

So, is any way exist to obtain or at least force to immediately store cookies?

1

There are 1 answers

3
hbk On BEST ANSWER

I ended with simple "force-like" saving Cookie from webpage.

To get all cookie i use

stringByEvaluatingJavaScriptFromString

with JS string like document.cookie();. As result i able to receive all cookies as a string with ; separator. All i need to do - parse string, create cookie and set it to NSHttpSharedStorage