检测 WKWebview 中的按钮点击 [英] Detect button click in WKWebview

查看:64
本文介绍了检测 WKWebview 中的按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 IOS 应用程序中,我创建了一个我不拥有的网站的 WKWebview.所以在我的里面:

In my IOS Application I've created a WKWebview of a website that I'm not owning myself. So inside my:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

我有几个 .evaluateJavascript 用于将数据添加到文本字段、删除网站上的元素等.

I've a couple of .evaluateJavascript that add data to textfields, removes elements on the website etc.

但我正在尝试添加一个 .evaluateJavascript(或其他)来识别网站上的按钮点击.

But I'm trying to add an .evaluateJavascript (Or other) that will recognize a button click on the website.

我尝试通过执行以下代码来添加它:

I've tried to add this by doing this code:

bookingView.evaluateJavaScript("document.getElementById('optional_button').onclick();") { (key, err) in
        if let err = err{
            print(err.localizedDescription)
        }
        else{
            print("You tapped the button!")
        }

但这行不通.每次我打开 WebView 时,应用程序都会打印出你点击了按钮",即使我没有.

But that doesnt work. Everytime I open up the WebView the app prints out "You tapped the button" even if I didn't.

那么,我可以使用evaluateJavascript检测按钮点击吗?

So, can I detect a button click with evaluateJavascript?

推荐答案

您可以捕获 url 并对其做出反应

you can catch the url and react to it

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(.allow)
        guard let urlAsString = navigationAction.request.url?.absoluteString.lowercased() else {
            return
        }

        if urlAsString.range(of: "the url that the button redirects the webpage to") != nil {
        // do something
        } 
     }

这篇关于检测 WKWebview 中的按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆