iOS
WKWebView

WKWebViewで表示したページのCookieを取得する

More than 1 year has passed since last update.

WKWebViewのdecidePolicyFor navigationResponseデリゲート内で処理

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

        for c in cookies {
            print(c.name + c.value)
        }
}