Blisko
Blisko

Reputation: 107

How can I invoke javascript functions from a wkwebview?

I'm using a wkwebview and when the user tap inside, I need to send back to the web server informations to allow it to make some actions :

The first function called btDialogClicked("ok" / "cancel") which is inside my completion handler action sheet alert :

Extension.createActionSheet(title: body["title"], message: body["message"], validationMessage: body["buttonOK"], cancelMessage: body["buttonCancel"], controller: self, completionHandler: nil)

And the second inside my case called btBackClicked which is inside the back case of my enum :

case .back:
backButton.imageView?.image = #imageLiteral(resourceName: "arrow_left")
break

How can I implement these two.

Upvotes: 0

Views: 1980

Answers (1)

MichaelV
MichaelV

Reputation: 1261

evaluateJavaScript() will allow you to run any JavaScript on webpage loaded by you WebView. Like:

webView.evaluateJavaScript("document.getElementById('myJSTextField').innerText") { (result, error) in
    guard error == nil, let result = result else {
       print("Error!")
    }
    print(result)
}

Upvotes: 1

Related Questions