WKWebView Cookies不能正常工作?

时间:2018-05-25 13:25:48

标签: swift cookies wkwebview

我在我的应用中使用WKWebView。我正在向webview发送网址请求。但是这个网址需要身份验证(cookies)。我正在为每个请求将Cookie传递给webview,但它在某些情况下工作正常,有些情况下它无效。这有什么不对?
我做了以下事情:

func loadDataOnWebViewWithCookies(_ urlString: String)  {
    let url = URL(string: urlString)
    let request = URLRequest(url: url!)
    if request.url != nil {
     var cookies: [String : String]? = nil
     if let anURL = request.url, let anURL1 = HTTPCookieStorage.shared.cookies(for: anURL) {
            cookies = HTTPCookie.requestHeaderFields(with: anURL1)
        }
     if cookies?["Cookie"] != nil {
            request.addValue(cookies!["Cookie"]!, forHTTPHeaderField: "Cookie")
        }
     }
    self.webView.load(request)
}

我使用下面的代表:

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        if webView.url?.absoluteString != "my_home_url" && webView.url?.host == Constants.kHost {
            let headerFields = navigationAction.request.allHTTPHeaderFields
            let headerIsPresent = headerFields?.keys.contains("Cookie")

            if headerIsPresent! {
                decisionHandler(.allow)
            } else {
                let request = NSMutableURLRequest(url: navigationAction.request.url!)
                var cookies: [String : String]? = nil
                if let anURL = navigationAction.request.url, let anURL1 = HTTPCookieStorage.shared.cookies(for: anURL) {
                    cookies = HTTPCookie.requestHeaderFields(with: anURL1)
                }
                request.allHTTPHeaderFields = cookies
                webView.load(request as URLRequest)

                decisionHandler(.cancel)
            }
        } else if webView.url?.host != Constants.kHost {
            let tuple = getTokenAndId()
            if navigationAction.request.value(forHTTPHeaderField: Constants.headersData.token) == tuple.xToken {
                decisionHandler(.allow)
            } else {
                let request = prepareRequestForHomeService(request: navigationAction.request)
                decisionHandler(.cancel)
                self.webView.load(request as URLRequest)
            }

        } else {
            decisionHandler(.allow)
        }
}

我在这里错了,请帮助我。 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

@Pallavi:请使用以下库在WKWebView中传递cookie。

https://github.com/Kofktu/WKCookieWebView

请查看以下代码以创建您的webview。

func configureWebView() {
    if webView == nil {
        let height = (self.navigationController?.isNavigationBarHidden)! ? self.view.frame.size.height - 20 : self.view.frame.size.height - 64
        webView = WKCookieWebView(frame: CGRect(x: xOrigin, y: yOrigin, width: self.view.frame.size.width, height: height), configurationBlock: { (configuration) in
            let processPool = WKProcessPool()
            configuration.processPool = processPool
        })
        webView.scrollView.delegate = self
        webView.wkNavigationDelegate = self

        webView.onDecidePolicyForNavigationAction = { (webView, navigationAction, decisionHandler)  in
             /// your code here                
        }

        webView.onDidReceiveChallenge = { (webView, navigationAction, completionHandler)  in
            /// your code here
        }

        webView.onDecidePolicyForNavigationResponse = { (webView, navigationResponse, decisionHandler)  in
           /// your code here
}