Swift webview: How to call correctly swift code from javascript?

后端 未结 2 1874
情深已故
情深已故 2020-12-12 17:31

I\'m trying to make my javascript interact with swift code but unfortunately i didn\'t succeed.

For the moment, i just tried to change the headers colors and displa

相关标签:
2条回答
  • 2020-12-12 18:13

    You have everything set up properly, but you aren't giving your WKWebViewConfiguration instance to the WKWebView. Since the configuration has the details of the Javascript/Swift bridge, you can't talk back and forth.

    override func loadView() {
        // ...
        var config = WKWebViewConfiguration()
        config.userContentController = contentController
    
        self.webView = WKWebView(frame: self.view.frame, configuration: config)
        self.view = self.webView!
    }
    
    0 讨论(0)
  • 2020-12-12 18:24

    my 2 cents, using a javascript callback with JSON ... for full class definition and layout, refer to adam's code

    import UIKit
    import WebKit
    
    class ViewController: UIViewController, WKScriptMessageHandler {
        var webView: WKWebView?
        ...
    

    then

    override func loadView() {
        let theConfiguration = WKWebViewConfiguration()
        let contentController = theConfiguration.userContentController
    
        // alert fix, at start to allow a JS script to overwrite it
        contentController.addUserScript( WKUserScript(
            source: "window.alert = function(message){window.webkit.messageHandlers.messageBox.postMessage({message:message});};",
            injectionTime: WKUserScriptInjectionTime.AtDocumentStart,
            forMainFrameOnly: true
        ) )
        contentController.addScriptMessageHandler(self, name: "messageBox")
    
        self.webView = WKWebView(frame: self.view.frame, configuration: theConfiguration)
    
        // and here things like: self.webView!.navigationDelegate = self
        self.view = self.webView!  // fill controllers view
    }
    

    and specifically

    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    
        if message.name == "messageBox" {
            let sentData = message.body as! Dictionary<String, String>
    
            let message:String? = sentData["message"]
    
            let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment:"btnOK"), style: .Default, handler: nil))
            self.presentViewController(alertController, animated: true) {}
        }
    }
    
    0 讨论(0)
提交回复
热议问题