我看了这个帖子,How can I obtain POST request body from WKWebView in Swift?。但它不会告诉您正在使用的是哪个JS脚本。我刚开始将JS注入到swift中,所以这篇文章对我来说并不清楚。
在wkwebview中加载请求后,我想获取post请求的http正文。什么才是获得它的正确方法。
发布于 2020-01-30 23:49:47
也许它与bug有关:https://bugs.webkit.org/show_bug.cgi?id=191362。在iOS 13中已经修复了这个问题。
发布于 2020-01-31 13:56:15
我找到了这个问题的解决方案。要获得post请求html的内容,我们只需要添加以下函数:-
func webView(_ webView: WKWebView,事件导航: WKNavigation!) {PerfomanceMonitor.stopEvent( didFinish:.startToLogin) hideActivityIndicator(delayed: true)
webView.evaluateJavaScript("(function() { for (var i = 0; i < document.forms.length; ++i) { if (typeof document.forms[i]['SAMLResponse'] !== 'undefined') return document.forms[i]['SAMLResponse'].value;} })();", completionHandler: { (jsonRaw: Any?, error: Error?) in
guard let jsonString = jsonRaw as? String
else
{
return
}
//use the extracted content for use.
})
}evaluateJavaScript接受javascript字符串作为参数,它只是您想要提取的html中内容的路径。我在这里使用了一个函数cz,我需要在所有元素的列表中找到一个内容。可以使用"document.documentElement.innerHTML“,它会给出完整的内容。
发布于 2020-01-30 14:36:08
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.request.httpBody)
}https://stackoverflow.com/questions/59979963
复制相似问题