Cookies not getting sync with WKHTTPCookieStore

Hi All,
In my scenario i need to open secured .aspx page in WKWebView. I'm doing it by setting required cookies to webview configuration instance while its creation. Its working fine and i'm able to open secured web page in WKWebView. But if i tried to open the page multiple times by moving back and fro, its getting failed every 5th or 6th time. However its working fine with UIWebView all the time.


Below is my implementation:

I have taken a global instance of my WKWebsiteDataStores e.g. in AppDelegate


Solution Reference: https://forums.developer.apple.com/message/335518#335518


//Inside my ViewController class

   //Calling
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.nonPersistentStore?.httpCookieStore.getAllCookies({ (cookies) in
          self.loadWebViewUsing(cookies: cookies)
    })

   //Load Request
    func loadWebViewUsing(cookies: [HTTPCookie]) {
        let webConfiguration: WKWebViewConfiguration = WKWebViewConfiguration()
        let groupDispatch = DispatchGroup()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let storage = WKWebsiteDataStore.nonPersistent()
        for cookie in cookies {
            groupDispatch.enter()
            storage.httpCookieStore.setCookie(cookie) {
                groupDispatch.leave()
            }
        }
        groupDispatch.notify(queue: DispatchQueue.main) {
            appDelegate.nonPersistentStore = storage
            webConfiguration.websiteDataStore = storage
            let newWebView = WKWebView(frame: self.view.frame, configuration: webConfiguration)
            newWebView.navigationDelegate = self
            self.view.insertSubview(newWebView, belowSubview: self.webView)
            self.webView.removeFromSuperview()
            self.webView = newWebView
            if let url = URL(string: "https://www.mysecuredpage.com") {
                self.webView.load(URLRequest(url: url))
            }
        }
    }


Debugging it using Safari Develop tool shows that getAllCookies returing me old cookies. Does it means my nonPersistent WKHTTPCookieStore going out of sync with the updated cookies.


Please suggest if i'm doing anything wrong or if this cookies sync issue with WKWebView is still a problem.

Test Environment: iOS 13.4 Simulator
regards,

Varun Mehta

I'm not sure this is critical for your issue, but why do you instantiate `WKWebsiteDataStore` and `WKWebView` at each time you call `loadWebViewUsing(cookies:)` ?

Once you get a WKWebsiteDataStore containing the required cookies, you can keep it somewhere and use it repeatedly.

Hi OOPer,

I'm instantiating WKWebView each time as we can not update configuration after its instatiation and i need to provide my updated configuration to WKWebView. This is an issue with WKWebView that i think has not yet fixed by Apple.
And for WKWebsiteDataStore, Yes i'm keeping it globally at one place , AppDelegate. Its just that i'm discarding the previous instance and assigning it the updated instance as it resolved one of my issue where website data store missed some of my previously setted cookies. Don't know why.

we can not update configuration after its instatiation and i need to provide my updated configuration to WKWebView

That means, if you do not update configuration, you have no need to instantiate WKWebView each time.


i'm discarding the previous instance and assigning it the updated instance as it resolved one of my issue

One possibility, you may have chosen a wrong solution, which may be causing this issue.

That means, if you do not update configuration, you have no need to instantiate WKWebView each time.

- Yes! But i have to update configuration each time i load the request with WKWebView.


One possibility, you may have chosen a wrong solution, which may be causing this issue.

- Don't know but this solution only worked for me. With others i couldn't even open the secured web page each time. Please suggest any solution if you have anything in mind.

The only solution I know is keeping the dataStore.

But my knowledge may be obsolete or not practical for actual servers.


Are there any public discussion to reach to your solution? I think I may need to learn what's happening.

Cookies not getting sync with WKHTTPCookieStore
 
 
Q