我有一个WKWebView
我用来修改网页,然后让用户与之互动。我希望能够使用javascript注入以编程方式单击按钮(document.getElementById("id").click())
,然后在Safari视图控制器中加载生成的网页。我熟悉WKWebView
和evaluateJavascript()
方法,但我不确定如何将以编程方式触发的按钮单击结果传输到Safari视图控制器。我该怎么做?我首先会使用Safari视图控制器,但似乎不支持注入脚本。
//various other modifications to webpage precede this
//target button is a 'next' button on webpage
//would like to "click" this button and open the next page in safari vc
//The code below opens the next page in WKWebView
let next = "document.getElementById('\(id)').click()"
webView.evaluateJavaScript(next) { (result, error) in
print("Result: \(result)")
print("Error: \(error)")
}