How to listen the button click action in a web page which loaded by webview?

As the title says, I want to listen to one button's action after the webpage has loaded. Is there any possible way to do that?

Replies

That depends on your relationship to the page. If you can change the content of the page, your best option is to configure the

onclick
handler on the button to run some JavaScript that calls your native code via the
WKScriptMessage
mechanism.

If you have no control over the page then you need to modify the page as it loads. In most cases you can do this using some combination of a

WKUserScript
script, configured to load after the document has finished loading (
WKUserScriptInjectionTime.atDocumentEnd
), and manually run JavaScript (
evaluateJavaScript(_:completionHandler:)
), but the details can get kinda tricky.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

it's this allowed if I write like this:

        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            if navigationAction.navigationType == WKNavigationType.linkActivated {
                print("link")
                
                decisionHandler(WKNavigationActionPolicy.cancel)
                return
            }
            print("no link")
            if((webView.url?.absoluteString.contains("https://secure5.arcot.com/vpas/admin/"))! && navigationAction.navigationType.rawValue == -1){
                print("user click the button")
            }
            decisionHandler(WKNavigationActionPolicy.allow)
            
        }

I think I'm in the second situation which you said before. The only thing which I know is to get the event values from WebKit navigation delegate.
The button which I want to listen is also the only button which exists on this web page. I will receive one navigation raw value and one URL value when I click on the button. But now I'm still unsure if this solution is secure enough.
If not, can you give me some example to show how to deal with this case?

I’m not sure what you’re asking here.

it's this allowed if I write like this:

Allowed by who?

But now I'm still unsure if this solution is secure enough.

And secure against what?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I mean, the code which I wrote it if is good enough or just some bad code. But at least I solved my issue by writing the code like this way.

if is good enough

That depends on your specific requirements. Earlier you mentioned that you wanted to “listen to one button's action”, which you can do via the techniques I’ve outlined. However, if your actual requirements are to intercept navigation to a specific page, then the navigation delegate approach you suggested is fine.

Well, the basic idea is fine but there are specific constructs I take issue with:

  • This code is problematic:

    webView.url?.absoluteString.contains("https://secure5.arcot.com/vpas/admin/"))!

    If you the

    url
    property is
    nil
    , you’re going to crash, so there’s no point using a
    ?
    and then, later on, using a
    !
    . A better option is to supply a default value:
    webView.url?.absoluteString.contains("https://secure5.arcot.com/vpas/admin/")) ?? false

    If

    url
    is nil, you won’t match.
  • Also, rather than use

    contains(_:)
    it’d be best to use
    .hasPrefix(_:)
    .
  • This construct is also problematic:

    navigationAction.navigationType.rawValue == -1

    The raw value of -1 corresponds to

    WKNavigationType.other
    , and you should use that rather than the raw value.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"