I created a method to set cookies from HTTPCookieStorage into WKHTTPCookieStorage, as it seems this api is full async so i created an async method that sets the cookies in wkHTTPCookieStorage.
func copyCookiesToWKStore(completion: @escaping () -> Void) {
if let httpCookies: [HTTPCookie] = self.httpCookieStorage.cookies {
print("updating wkCookies with httpCookies: \(httpCookies)")
func copyCookiesAsync(index: Int, cookieHandler: SDCookiesHandler?) {
if let cookieHandler = cookieHandler, index < httpCookies.count {
let cookie = httpCookies[index]
cookieHandler.wkCookieStore.setCookie(cookie, completionHandler: {
print("set cookie in Cookie Storage: \(cookie)")
DispatchQueue.main.async {
copyCookiesAsync(index: index + 1, cookieHandler: cookieHandler)
}
})
}
else {
completion()
}
}
weak var cookieHandler = self
copyCookiesAsync(index: 0, cookieHandler: cookieHandler)
} else {
completion()
}
}
and the load the request in the wkwebview:
wkCookiesHandler.copyCookiesToWKStore { [weak self] in
if let weakSelf = self {
DispatchQueue.main.async {
weakSelf.webView.load(cookiesRequest)
}
}
However when webview calls the decidePolicyFor navigationAction method, the cookies in WKCookieStorage are not fully synced at this point.
I have read in some posts that WKProcessPool has its own cycle to sync cookies, and that resetting it, causes cookies to sync with wkwebview, but i found that i am in existent webview flow, it causes weird artifacts with the webview.
Has anyone started using WKCookieStorage api, and how can one keep cookies inserted in this api, visible by the WKWebview.