I am sorry, if this question was already asked, but all the answers I could find are for complicated solutions.
I have a viewController, which is webView. I added WKWebView, to my view from WebKit library, and opened simple url. Inside of my ViewController, I have a native function called showAd.
When the user press the button, which was implemented in javascript, it show call showRewardsAd. I set the break point to the userContentController, however, it never goes there.
func showAd(){
print("invoked")
}
extension WFWebViewController: WKScriptMessageHandler{
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "showRewardsAd"{
self.showRewards()
webView.evaluateJavaScript("test", completionHandler: nil)
}
}
}
My goal is to simply allow this function to be called from javascript part. As I know, if I want to call javascript function in swift, I simply have to say:
webview.addJavascriptInterface('javascript: function_name();')
However, in my case, I want to do it in reverse order. Call swift function in Javascript code.