How can i monitor requests on WKWebview?

后端 未结 4 980
情书的邮戳
情书的邮戳 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:54

    If you have control of the content inside the WkWebView you can send messages to your native app using window.webkit.messageHandlers whenever you make an ajax request, which will be received as a WKScriptMessage that can be processed by whatever you've designated as your WKScriptMessageHandler. The messages can contain whatever information you wish, and will be automatically converted into native objects/values in your Objective-C or Swift code.

    If you don't have control over the content you can still do this by injecting your own JavaScript via a WKUserScript to track ajax requests and send back messages using the method stated above.

提交回复
热议问题