为什么我的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里能运行

阅读 5.3k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题