when redirect with code 302, WKWebView cannot set cookie

前端 未结 7 1458
被撕碎了的回忆
被撕碎了的回忆 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:43

    Another solution worked for me:

    Adopt WKNavigationDelegate, webView.navigationDelegate = self, then:

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        /// Cancel the navigation action
        decisionHandler(.cancel)
    
        /// Navigate yourself 
        let request = NSMutableURLRequest(url: url)
        request.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: Auth.getAuthCookies())
        webView.load(request as URLRequest)
    }
    

    This was all that was needed for my simple case, but I imagine others may also need to copy the other request info that's stored in navigationAction.request like the navigationAction.request.allHTTPHeaderFields

提交回复
热议问题