meismyles / SwiftWebVC

A drop-in inline browser for your Swift iOS app.
MIT License
329 stars 117 forks source link

handle js #34

Closed Cj370118568 closed 7 years ago

Cj370118568 commented 7 years ago

inSwiftWebVC.swift, i have added config as follw

public class SwiftWebVC: UIViewController,WKScriptMessageHandler {
public var jsHandler:((WKScriptMessage)->())?
...
}
lazy var webView: WKWebView = {
        let configuration = WKWebViewConfiguration()
        configuration.userContentController.add(self, name: "wawa")
        var tempWebView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
        tempWebView.uiDelegate = self
        tempWebView.navigationDelegate = self
        return tempWebView;
    }()
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if self.jsHandler != nil {
            self.jsHandler!(message)
        }
    }

and it just fail to work, maybe i need help.

Cj370118568 commented 7 years ago

actually, it works. my colleagues make a mistake. code like this window.webkit.messageHandlers.wawa.postMessage() can't work, when he change this to window.webkit.messageHandlers.wawa.postMessage('test') it work.