Swift - Cookie not set in WKWebView

162 views Asked by At

I'm trying to set a cookie in the WKWebView, but the web app can't read it (document.cookie).

I tried to set secure to false. That only works if I set it like in following code. Also tried to set the httpOnly attribute to false. But if I print the cookie, it has the value YES

        let cookieProps: [HTTPCookiePropertyKey : Any] = [
            HTTPCookiePropertyKey.domain: "192.168.1.2",
            HTTPCookiePropertyKey.path: "/",
            HTTPCookiePropertyKey.name: "testCookie",
            HTTPCookiePropertyKey.value: "test",
            HTTPCookiePropertyKey.expires: "2024-12-17 07:26:41 +0000",
            HTTPCookiePropertyKey.init(rawValue: "HttpOnly"): false,
            HTTPCookiePropertyKey.init(rawValue: "secure"): false
        ]

        return HTTPCookie(properties: cookieProps) ?? HTTPCookie()

After creating the cookie, I set it with the cookie store:

var configuration = WKWebViewConfiguration()
configuration.websiteDataStore.httpCookieStore.setCookie(cookie, completionHandler: nil)
0

There are 0 answers