如何在 Swift 4 中从 WKWebView 获取 cookie 值?

Posted

技术标签:

【中文标题】如何在 Swift 4 中从 WKWebView 获取 cookie 值?【英文标题】:How to fetch cookies values from WKWebView in Swift 4? 【发布时间】:2018-09-16 16:51:36 【问题描述】:

我无法从该网站获取 cookie -“https://bødekontrol.dk” 我正在使用 Xcode 9.4.1 和 ios 11。 我已经按照下面的代码,

import UIKit
import WebKit
class ViewController: UIViewController 
    var urlString = "https://bødekontrol.dk"
    var webView: WKWebView!
    fileprivate var webViewIsInited = false

    override func viewWillLayoutSubviews() 
        if !webViewIsInited 
            webViewIsInited = true
            if webView == nil 
                webView = WKWebView(frame: UIScreen.main.bounds, configuration: WKWebViewConfiguration())
            

            view.addSubview(webView)
            webView.navigationDelegate = self
            webView.uiDelegate = self
            webView.loadUrl(string: urlString)
        
    

extension ViewController: WKNavigationDelegate 

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) 
        decisionHandler(.allow)
        if let httpResponse = navigationResponse.response as? HTTPURLResponse 
            if let headers = httpResponse.allHeaderFields as? [String: String], let url = httpResponse.url 
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: headers, for: url)

                for cookie in cookies 
                    print(cookie.description)

                    print("found cookie " + cookie.name + " " + cookie.value)
                
            
        
    

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) 
        print("didFinish navigation")

    


extension ViewController: WKUIDelegate 

    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? 
        if navigationAction.targetFrame == nil 
            let vc = ViewController()
            vc.urlString = navigationAction.request.url?.absoluteString ?? "http://google.com"
            vc.view.frame = UIScreen.main.bounds
            vc.webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
            navigationController?.pushViewController(vc, animated: false)
            return vc.webView
        
        return nil
    


extension WKWebView 

    func loadUrl(string: String) 
        if let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
            let url = URL(string: encoded)
        
            if self.url?.host == url.host 
                self.reload()
             else 
                load(URLRequest(url: url))
            
        
    

【问题讨论】:

是否调用了导航委托方法来收集 cookie? 【参考方案1】:

实现这个协议WKHTTPCookieStoreObserver并检查这个协议的功能。

cookiesDidChange

【讨论】:

非常感谢。您的建议很有帮助。

以上是关于如何在 Swift 4 中从 WKWebView 获取 cookie 值?的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中从 WKWebview 获取 HTML

如何在 swift 4 中从 UIImage 读取 exif 数据?

如何在swift 4中从数组中添加标题

如何在 swift 4 中从图像选择器获取照片本地 url

Swift 4/5 设置将代理添加到 WKWebview

使用 Swift 4 滚动 WKWebview