Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script message handler not working

I am trying to inject a custom WebKit script message handler. My controller is correctly loaded and the view as well. However, on the JS side (from the Safari console), window.webkit.messageHandlers is empty. Any idea why? My app is using iOS 8.1 but even by upgrading to 9.2, it did not work.

import Foundation
import UIKit
import WebKit

public class FooController: UIViewController, WKScriptMessageHandler {
    private var wkWebView: WKWebView?

    public override func viewDidLoad() {
        super.viewDidLoad()

        let o = WKUserContentController()
        o.addScriptMessageHandler(self, name: "foo")
        let config = WKWebViewConfiguration()
        config.userContentController = o

        self.wkWebView = WKWebView(frame: self.view.bounds, configuration: config)
        self.view.addSubview(self.wkWebView!)

        self.wkWebView!.loadRequest(NSURLRequest(URL: NSBundle.mainBundle().URLForResource("foo", withExtension: "html")!))
    }

    public func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        print("foo")
    }
}
like image 748
Adrien Cadet Avatar asked Jan 12 '16 00:01

Adrien Cadet


Video Answer


1 Answers

webkit.messageHandlers is not an Array. It is a UserMessageHandlersNamespace.

Try

var message = {"key1":"value1", "key2":"value2", "dictionary": {"name": "foo"}}
webkit.messageHandlers.foo.postMessage(message);

In your message handler.

public func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    let body = message.body
    if let dict = body as? Dictionary<String, AnyObject> {
        print(dict)
    }
}
like image 162
Onato Avatar answered Oct 10 '22 12:10

Onato