How can i monitor requests on WKWebview?

后端 未结 4 994
情书的邮戳
情书的邮戳 2020-12-02 07:15

How can i monitor requests on WKWebview?

I\'v tried using NSURLprotocol (canInitWithRequest) but it won\'t monitor ajax requests (XHR), only navig

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 07:46

    @Benzi Heler answer is great, but it uses jQuery which seems like is not working in WKWebView anymore, so I have found solution without using jQuery.

    Here is ViewController implementation that lets you be notified every AJAX request is completed in WKWebView:

    import UIKit
    import WebKit
    
    class WebViewController: UIViewController {
    
        private var wkWebView: WKWebView!
        private let handler = "handler"
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let config = WKWebViewConfiguration()
            let userScript = WKUserScript(source: getScript(), injectionTime: .atDocumentStart, forMainFrameOnly: false)
            config.userContentController.addUserScript(userScript)
            config.userContentController.add(self, name: handler)
    
            wkWebView = WKWebView(frame:  view.bounds, configuration: config)
            view.addSubview(wkWebView)
    
            if let url = URL(string: "YOUR AJAX WEBSITE") {
                wkWebView.load(URLRequest(url: url))
            } else {
                print("Wrong URL!")
            }
        }
    
        private func getScript() -> String {
            if let filepath = Bundle.main.path(forResource: "script", ofType: "js") {
                do {
                    return try String(contentsOfFile: filepath)
                } catch {
                    print(error)
                }
            } else {
                print("script.js not found!")
            }
            return ""
        }
    }
    
    extension WebViewController: WKScriptMessageHandler {
        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            if let dict = message.body as? Dictionary, let status = dict["status"] as? Int, let responseUrl = dict["responseURL"] as? String {
                print(status)
                print(responseUrl)
            }
        }
    }
    

    Pretty standard implementation. There is a WKWebView created programmatically. There is injected script that is loaded from script.js file.

    And the most important part is script.js file:

    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener("load", function() {
            var message = {"status" : this.status, "responseURL" : this.responseURL}
            webkit.messageHandlers.handler.postMessage(message);
        });
        open.apply(this, arguments);
    };
    

    userContentController delegate method will be called every time there is AJAX request loaded. I'm passing there status and responseURL, because this was what I needed in my case, but you can also get more informations about request. Here is the list of all properties and methods available: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

    My solution is inspired by this answer written by @John Culviner: https://stackoverflow.com/a/27363569/3448282

提交回复
热议问题