最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Call native swift function in javascript - Stack Overflow

programmeradmin1浏览0评论

I am sorry, if this question was already asked, but all the answers I could find are for plicated 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", pletionHandler: 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.

I am sorry, if this question was already asked, but all the answers I could find are for plicated 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", pletionHandler: 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.

Share Improve this question edited Oct 16, 2020 at 8:50 gcharita 8,3773 gold badges28 silver badges43 bronze badges asked Oct 16, 2020 at 5:16 Yessen YermukhanbetYessen Yermukhanbet 1051 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You need to inject your user script inside the webView so you could listen to JavaScript's messages when they are posted.

Firstly, you will need to initialize the webView's config and preferences, to allow javaScript:

let config = WKWebViewConfiguration()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
config.preferences = preferences

Now, you need to inject your script to listen for the click events from the webView:

let userController = WKUserContentController()
let javaScript = "showRewardsAd = function() {\n" + "window.webkit.messageHandlers.adClicked.postMessage(); }"
let script = WKUserScript(source: javaScript, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
userController.addUserScript(script)
userController.add(self, name: "adClicked")
config.userContentController = userController

Now instantiate your webView with the configuration from above:

self.webView = WKWebView(frame: .zero, configuration: config)

Now, your WKScriptMessageHandler protocol is implemented correctly:

extension WFWebViewController: WKScriptMessageHandler{
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "showRewardsAd"{
            self.showRewards()
            print("Ad is being shown.")
        }
    }
}

I hope this works for you. I assumed that showRewardsAd is implemented in the JavaScript on the website which you're accessing.

发布评论

评论列表(0)

  1. 暂无评论