0

I'm using a WKWebview on my story board. I need a way for clicking on specific text to perform an action.

Assume I have a webkit added:

@IBOutlet weak var webkit: WKWebView!

And then later on I have:

webkit.loadHTMLString("Click here or there for different actions", baseURL: nil)

How could I set this up so that clicking on the word "here" would perform code of my choosing, and clicking on "there" would perform different code of my choosing.

It needs to be set up in a webkit like this because I need to use html (the above is just an example).

I had thought about trying to do this through linking. For example:

webkit.loadHTMLString("Click <a href=''>here</a> or there for different actions", baseURL: nil)

And then run something like I found in this question's answer:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == WKNavigationType.linkActivated {

        print("run code")

        decisionHandler(WKNavigationActionPolicy.cancel)
        return
    }      
    decisionHandler(WKNavigationActionPolicy.allow)
}

However this seems messy as I don't actually need website links. And this only works for one specific action. Clicking on any link would allow me to run some kind of code, but I want specific code for specific words.

Any guidance would be greatly appreciated!

Adam S.
  • 173
  • 3
  • 15

0 Answers0