3

I have a button in my Swift project,

Can I click the HTML button with the Swift button?

This is my Swift button:

@IBAction func buttonclick(_ sender: UIButton) {
    //click html button when click swift button
}

This is my HTML code:

<a id="htmlbutton" class="buton" onClick="clickbutton('data')" >Click</a>

I use wkWebView in my project and this html code from my website

Sorry for my bad English.

please check the image

screenshot

aduguid
  • 3,099
  • 6
  • 18
  • 37
EagleH
  • 107
  • 2
  • 8
  • Possible duplicate of [Convert HTML to NSAttributedString in iOS](https://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in-ios) – Do2 Jun 23 '18 at 15:49
  • Hi @Do2 I have found the similar question but there is no solution https://stackoverflow.com/questions/47523277/how-to-click-a-button-in-javascript-with-swift – EagleH Jun 23 '18 at 15:56
  • Did you read the answers on my link? – Do2 Jun 23 '18 at 16:00
  • Yes, I did. I just want to click button in my website from swift button. – EagleH Jun 23 '18 at 16:04
  • How would you do that? I don't understand. Give me an example... – Do2 Jun 23 '18 at 16:07
  • I have added an image to my question. please check it. – EagleH Jun 23 '18 at 16:46
  • Check this article, i think it will help you https://mislavjavor.github.io/2016-03-08/WKWebView-advanced-tutorial/ good luck! – Do2 Jun 23 '18 at 18:04

1 Answers1

2

You can manually trigger a click on the HTML button by injecting and running a JS script using WKWebView's evaluateJavaScript method. The script might look like this: document.getElementById('htmlbutton').click();.

dr_barto
  • 5,723
  • 3
  • 26
  • 47