title: How to Click Specific Text in a WKWebView to Perform an Action in Swift
body: <p>I'm using a WKWebview on my story board. I need a way for clicking on specific text to perform an action.</p>
  
  <p>Assume I have a webkit added:</p>
  
  <pre><code>@IBOutlet weak var webkit: WKWebView!
  </code></pre>
  
  <p>And then later on I have:</p>
  
  <pre><code>webkit.loadHTMLString("Click here or there for different actions", baseURL: nil)
  </code></pre>
  
  <p>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.</p>
  
  <p>It needs to be set up in a webkit like this because I need to use html (the above is just an example).</p>
  
  <p>I had thought about trying to do this through linking. For example:</p>
  
  <pre><code>webkit.loadHTMLString("Click &lt;a href=''&gt;here&lt;/a&gt; or there for different actions", baseURL: nil)
  </code></pre>
  
  <p>And then run something like I found in this <a href="https://stackoverflow.com/a/46254295/9195178">question's answer</a>:</p>
  
  <pre><code>func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -&gt; Void) {
      if navigationAction.navigationType == WKNavigationType.linkActivated {
  
          print("run code")
  
          decisionHandler(WKNavigationActionPolicy.cancel)
          return
      }      
      decisionHandler(WKNavigationActionPolicy.allow)
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any guidance would be greatly appreciated!</p>