How to synchronize cookies in WKWebview

We have changed from UIWebView we used to WKWebview.

Changing WebView from UIWebView to WKWebView prevents login.

Regenerate the PHPSESSID on the server in the login processing of the Web application and reconfigure it as a cookie.

Because ios side can not confirm the update, I think that there is a problem with cookie synchronization processing.

How do I synchronize cookies in WK WebView?


◆Configuration

【ios】   ios version: iOS9 Xcode:8.3.3 Swift3

【Server】 WebServer:CentOS framework:cakePHP 2.9.9(Using authComponent)


◆Steps to Reproduce

1.Login authentication with WebAPI

 ⇒PHPSESSID is returned from the server

  Set PHPSESSID to HTTPCookieStorage.shared.cookies

2.Generate WebView with WKWebview  

⇒PHPSESSID returned from the server is set to WKWebView's configuretion using WKUserScript

3.application login processing  

⇒Loading PHPSESSID obtained from server with MutableURLRequest.  

 ※At this time, the server regenerates PHPSESSID and reconfigures it as a cookie

4.Screen transition  

⇒It redirects to the login screen of the web application ★a problem occurred   

In UIWebView, it transits to the screen after login normally.


Supplement:

The PHPSESSID is not updated even if check the cookie

(HTTPCookieStorage and navigationAction.request.allHTTPHeaderField) at the timing of webView (_: decidePolicyFor: decisionHandler :).


Thank you

Replies

WKWebView
runs all of its networking in a separate process and thus does not ‘see’ your process’s cookie store. In iOS 11 we added
WKHTTPCookieStore
to give you full access to the web view’s cookie store. I recommend that you start by getting that working then, if you need to support versions of iOS, we can talk about workarounds.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for your answer. I understood that WKWebview does not recognize the cookie store of the process. It is difficult to handle WKWebview (cookie store) on iOS 9. I am considering support for iOS 11, WKWebview and WKHTTP cookie store will work properly on iOS 10 or earlier devices? Please let me know if there is a problem with iOS version. Thank you

Supported cookie sync with

WKWebView
on older platforms is tricky. There are two techniques that might work:
  • You can set a cookie in the headers of the request you pass to

    -[WKWebView loadRequest:]
    .
  • You can get and set cookies from within the web view by running JavaScript code (using

    -evaluateJavaScript:completionHandler:
    ) that accesses the JavaScript
    document.cookie
    value.

However, these approaches might not working depending on the specifics of your cookies.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you so much. I tried the technique that you taught, but unfortunately the synchronization of cookies did not work well. I would like to think about other methods.

I to had the same issues with this WKHTTPCookieStorage, i created a wrapper method that would add an array of cookie async and on completion it would load the URLRequest in the WKWebView, however when it gets into decidePolicyFor navigationAction method the wkwebview cookies are not the ones i set before in WKHTTPCookieStorage, i believe WKProcessPool is the one responsible for the cookies to be in sync, and it seems that settings cookies in a webview that is created and has a configuration, does not mean that they will be in the wkwebview when the webpage starts loading. i actually created a post with what seems to be a similar issue to yours.


https://forums.developer.apple.com/thread/97194


It would be good to know if this is by design, or is it potentially a bug. If it is by design why does this happen, and what workarounds can be used.

It would be good to know if this is by design, or is it potentially a bug. If it is by design why does this happen, and what workarounds can be used.

My recommendation here is that you open a DTS tech support incident for this. I’m happy to look into it, but I just don’t have enough time for it in the context of DevForums.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have met another question with wkwebview, I press home button to deactive app using webview, when I reactive the app and press go back button to navigate wkwebview, the wkwebview reload h5 url automactically, this only occurs when I deactive the app and reopen it, how can I prevent it to reload h5, thx.

As this question is unrelated to cookies, you should start a new thread for it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hey guy, how can you fix the bug.


How to synchronize cookie in WKWebview of earlier than ios 11


Could you show me the code? Thanks !

My post from 15 Jan explains the options you have prior to iOS 11. I recommend that you try these out and come back to us if you have specific questions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The WKHTTPCookieStore not working on iOS 11.3 I tried below options but nothing worked for me


let auc = HTTPCookie.init(properties: [HTTPCookiePropertyKey.name: "OAuth.AccessToken", HTTPCookiePropertyKey.value: authToken, HTTPCookiePropertyKey.domain: ".myDomain.com", HTTPCookiePropertyKey.path: "/", HTTPCookiePropertyKey.secure: "TRUE"])

// Here I tried two options, setting the cookie in configuration before the webview
// is created and also after the webview is created.
if let ac = auc {
    if #available(iOS 11.0, *) {
        webView.configuration.websiteDataStore.httpCookieStore.setCookie(ac) {
            DispatchQueue.main.async {
                self.webView.load(checkoutRequest)
            }
        }
    } else {
        // fallback
    }
}