繁体   English   中英

如何从 WKWbView 获取标题完成加载

[英]How to get headers from WKWbView finish loads

我在应用程序中使用WKWebView & 我正在寻找从 wkwebview 中获取所有标题的完成加载方法webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {就像在UIWebView

func webViewDidFinishLoad(_ webView: UIWebView) {
        print("Finished loads---", webView.request?.allHTTPHeaderFields)
    }

我们如何在WKWebView实现这WKWebView

您需要将视图控制器设置为 WKWebView 导航委托WKNavigationDelegate并实现其decidePolicyFor方法:

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

像这样尝试:


func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    let allHTTPHeaderFields = navigationAction.request.allHTTPHeaderFields ?? [:]
    for (key, value) in allHTTPHeaderFields {
        print("key:", key, "value:", value)
    }
    
    if navigationAction.navigationType == .linkActivated  {
        if let url = navigationAction.request.url,
            let host = url.host, !host.hasPrefix("www.google.com"),
            UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
            print(url)
            print("Redirected to browser. No need to open it locally")
            decisionHandler(.cancel)
        } else {
            print("Open it locally")
            decisionHandler(.allow)
        }
    } else {
        print("not a user click")
        decisionHandler(.allow)
    }
}

使用WKWebViewwebView(_:decidePolicyFor:decisionHandler:)

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    // MARK: - IBOutlet
    @IBOutlet weak var webView: WKWebView!
    
    
    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let urlStr = "https://www.google.com/"
        if let url = URL(string: urlStr) {
            let request = URLRequest(url: url)
            webView.load(request)
            //webView.allowsBackForwardNavigationGestures = false
            //webView.navigationDelegate = self
        }
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        let request = navigationAction.request
        if let host = navigationAction.request.url?.host {
            if host == "www.google.com" {
                if let headers = request.allHTTPHeaderFields {
                    print("Google Headers: \(headers)")
                } else {
                    print("Nope, sir")
                }
            }
            else if host == "www.apple.com" {
                if let headers = request.allHTTPHeaderFields {
                    print("Apple Headers: \(headers)")
                } else {
                    print("Nope, sir")
                }
            }
        }
        decisionHandler(.allow)
    }
}

我得到以下信息。

["User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)", "Accept": "text/html,application/xhtml+xml ,应用/ XML; q = 0.9,/ q = 0.8 “]接头:[”接受-语言“: ”EN-US“, ”升级不安全-请求“: ”1“, ”接受编码“:” gzip, deflate, br", "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)", "Accept": "text/html ,应用/ XHTML + xml的,应用/ XML; q = 0.9,/ q = 0.8" ]

①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM