when redirect with code 302, WKWebView cannot set cookie

前端 未结 7 1457
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 02:59

I sent request to url1. url1 will redirect to url2 with cookie. url2 is for authorization. And I get code \"302 found\", which is correct. But when url2 redirect back to ur

相关标签:
7条回答
  • 2020-12-18 03:55

    recently i faced same problem and i fixed this problem by doing this.I am sure cookie lost happen only if you are using iOS < 11, for this you need to apply some work around as i applied recently.

    1- pass updated cookies in response from server side.Whenever it changed from server,

    Read cookie from response like this way.

    if  let allHttpHeaders = response.allHeaderFields as? [String: String] {
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: allHttpHeaders, for: responseUrl)
                for cookie in cookies {
                    if cookie.name == "cookie name" {
                        // compare with old cookie value so it will show your cookie is changed now.
                        if "your_old_cookie_value" != cookie.value {
    
                                //update cookie in Script and request header
                                // reload your page here again.
                            break
                        }
                    }
                }
            }
    

    This is the minimal change by that you can fix cookie lost issue, for iOS >=11 should not be an issue, for ios 11 and above use httpcookiestore

    Let me know if you have any question.

    0 讨论(0)
提交回复
热议问题