WKWebview datastore not returning all cookies that were set

Hi,


We are using WKWebview to load mobile webpages in our app. While loading a particular mobile webpage, the response sets certain cookies.

Later we need to call a rest api using the stored cookies. So, we call the below method to get all the cookies. But what we notice is some cookies that was suppose to be set after we got response goes missing. The behaviour is quite intermittent. Sometimes those cookies are seen. And sometimes not.


[self.webView.configuration.websiteDataStore.httpCookieStore getAllCookies:^(NSArray<NSHTTPCookie *> * cookies) {

NSLog(@"cookies before:%@", cookies);

}];


What could be the possible reason for this kind of behaviour.

Hi

Even I am facing the issue any updated on this?
Same issue here.
I have same issue, do we have any updates/work around for this issue ?
I already implement with singleton WKProcessPool and pass here when initialize this WKWebviewViewController. Here is a sample logic I used to check the cookies from an website url after it finishes the navigation, cookies does not there

import UIKit
import WebKit

class WKWebviewViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

  var urlToLoad: String = "<any website url, I even try with apple.com>"
  var cookiesTxt: String!
var processPool: WKProcessPool!
  var wkWebviewconfigure: WKWebViewConfiguration = WKWebViewConfiguration()

  var webView: WKWebView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // webView programatically

    let bounds = self.view.bounds
    let frame = CGRect(x: bounds.origin.x, y: bounds.origin.y + 100, width: bounds.width, height: bounds.height)
   wkWebviewconfigure.processPool = processPool
    self.webView = WKWebView(frame: frame, configuration: wkWebviewconfigure)
    self.view.addSubview(self.webView)

    webView.navigationDelegate = self
    webView.uiDelegate = self
    if let url = URL(string: urlToLoad) {
      let request = URLRequest(url: url)
      DispatchQueue.main.async {
        self.webView.load(request)
      }
    }
  }
   public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    let httpCookieStore = webView.configuration.websiteDataStore.httpCookieStore
    httpCookieStore.getAllCookies { (cookies) in
      print(cookies) // <--- not getting all the cookies most of time here, but sometime it works
    }
  }
}
After a week struggling, I found a way to make it work. Posting here in case anyone need.

#1 Have an instance of processPool and wesiteDataStore.nonpersistent() (make them a global variables if you want to get the Cookies outside of webview didFinish navigation of the same wkWebview)

#2 Create an instance of wkWebviewconfigure with these two:
var wkWebviewconfigure: WKWebViewConfiguration = WKWebViewConfiguration()
wkWebviewconfigure.processPool = myProcessPool
wkWebviewconfigure.websiteDatastore = myWebsiteDatastore // <--- have to be nonpersistent type

#3 Generate webview from code with this wkWebviewconfigure, assigning wkWebviewconfigure to your wkWebview.configuration from .xib and storyboard will not work

WKWebview datastore not returning all cookies that were set
 
 
Q