我正在使用 WKWebView
来展示一个包含三个按钮的HTML网站。当特定按钮被点击时,我想在本地应用程序中运行一些Swift代码。
关于这段HTML
这三个按钮看起来像这样:
<input type="button" value="Edit Info" class="button" onclick="javascript:GotoURL(1)">
<input type="button" value="Start Over" class="button" onclick="javascript:GotoURL(2)">
<input type="button" value="Submit" class="button" onclick="javascript:GotoURL(3)">
他们调用的 GotoURL
函数看起来像这样:
function GotoURL(site)
{
if (site == '1')
document.myWebForm.action = 'Controller?op=editinfo';
if (site == '2')
document.myWebForm.action = 'Controller?op=reset';
if (site == '3')
document.myWebForm.action = 'Controller?op=csrupdate';
document.myWebForm.submit();
}
当前WKWebView
实现
当我在webview中点击任何按钮时,我的WKNavigationDelegate
会调用此函数:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
// ...?
}
当然,navigation
是不透明的,因此不包含有关用户单击了三个按钮中的哪一个的信息。
检测此按钮何时被点击的最简单方法是什么?
我希望在用户单击提交时做出响应,并忽略其他按钮的按下。
我在Stack Overflow上看到一些使用WKUserContentController
的其他方法,但它们似乎需要该网站调用类似以下内容的东西:
window.webkit.messageHandlers.log.postMessage("submit");
我无法控制这个网站,因此无法在其源代码中添加此行,并且我不知道使用WKWebView
将其注入到正确位置的最佳方法。