首页 > 为什么我的js不能在swift的webview里运行?

为什么我的js不能在swift的webview里运行?

webview我是这么写的

override func webView(sender: WebView!, didClearWindowObject windowObject: WebScriptObject!, forFrame frame: WebFrame!) {
    if (webView.mainFrameDocument != nil) { // && frame.DOMDocument == webView.mainFrameDocument) {
        let document:DOMDocument = webView.mainFrameDocument
        let hangoutsMode: String? = Preferences.getString("hangoutsMode")
        let windowScriptObject = webView.windowScriptObject;
        windowScriptObject.setValue(self, forKey: "gInbox")
        windowScriptObject.evaluateWebScript("console = { log: function(msg) { gInbox.consoleLog(msg); } }")

        let path = NSBundle.mainBundle().pathForResource("gInboxTweaks", ofType: "js", inDirectory: "Assets")
        let jsString = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
        let script = document.createElement("script")
        let jsText = document.createTextNode(jsString)
        let bodyEl = document.getElementsByName("body").item(0)

        script.setAttribute("type", value: "text/javascript")
        script.appendChild(jsText)
        bodyEl?.appendChild(script)

        windowScriptObject.evaluateWebScript(String(format: "console.log('test'); hangoutsMode(%@)", hangoutsMode!))
    }
}

在oc里能运行


可以看一下这的解释:
链接地址

【热门文章】
【热门文章】