2016-12-06 53 views
1

我有一個WBWebview運行到NSPopover
我已經關注了this指南,以便能夠將數據發送回JS的Swift應用程序。 不幸的是,userContentController永遠不會在Swift應用程序中調用。 這裏是我的斯威夫特應用程序的視圖控制器代碼:userContentController從未從JS注入回撥

class GSViewController: NSViewController, WKScriptMessageHandler, WKNavigationDelegate { 

    var webView: WKWebView? 

    var webConfig:WKWebViewConfiguration { 
     get { 

      // Create WKWebViewConfiguration instance 
      let webCfg:WKWebViewConfiguration = WKWebViewConfiguration() 

      // Setup WKUserContentController instance for injecting user script 
      let userController:WKUserContentController = WKUserContentController() 

      // Add a script message handler for receiving "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message 
      userController.addScriptMessageHandler(self, name: "buttonClicked") 

      // Get script that's to be injected into the document 
      let js:String = buttonClickEventTriggeredScriptToAddToDocument() 

      // Specify when and where and what user script needs to be injected into the web document 
      let userScript:WKUserScript = WKUserScript(source: js, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: false) 

      // Add the user script to the WKUserContentController instance 
      userController.addUserScript(userScript) 

      // Configure the WKWebViewConfiguration instance with the WKUserContentController 
      webCfg.userContentController = userController; 

      return webCfg; 
     } 
    } 

    override func loadView() { 
     super.loadView() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.webView = WKWebView(frame: self.view.frame, configuration: webConfig) 
     self.webView!.navigationDelegate = self 
     self.view = webView! 

     let username = NSUserName() 
     let url = NSURL(string:"someUrl") 
     let req = NSURLRequest(URL:url!) 
     self.webView!.loadRequest(req) 
     self.view.frame = CGRectMake(0, 0, 440, 600) 
    } 

    func buttonClickEventTriggeredScriptToAddToDocument() ->String{ 
     let script:String = "webkit.messageHandlers.callbackHandler.postMessage('Does it works?');" 
     return script; 

    } 

    // WKNavigationDelegate 
    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) { 
     NSLog("%s", #function) 
    } 

    func webView(webView: WKWebView, didFailNavigation navigation: WKNavigation!, withError error: NSError) { 
     NSLog("%s. With Error %@", #function, error) 
     showAlertWithMessage("Failed to load file with error \(error.localizedDescription)!") 
    } 

    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { 
     print("called") 
     if(message.name == "callbackHandler") { 
      print("It does ! \(message.body)") 
     } 
    } 

    // Helper 
    func showAlertWithMessage(message:String) { 
     let myPopup:NSAlert = NSAlert() 
     myPopup.messageText = message 
     myPopup.alertStyle = NSAlertStyle.WarningAlertStyle 
     myPopup.addButtonWithTitle("OK") 
     myPopup.runModal() 
    } 

} 

我想直接注入調用雨燕回調webkit.messageHandlers.callbackHandler.postMessage('Does it works?');,但它似乎沒有工作。

通過不同的測試,我發現注入實際上是以Swift - > JS方式工作的(我已經能夠通過注入JQuery代碼修改可見的HTML元素的CSS),但JS - > Swift橋似乎並沒有做到這一點。 有沒有人有一個想法,爲什麼?

我在OSX 10.11.6下運行XCode 7.3.6。

我真的很喜歡Swift和OSX編程,所以毫不猶豫地指出我錯過的任何元素。我自願地省略了我的頁面使用的JS,因爲我在示例中沒有使用它。

謝謝。

回答

0

發現它,完全是我的壞。 當我將腳本添加到webview配置時,我忘了重命名消息處理程序。

userController.addScriptMessageHandler(self, name: "callbackHandler") // was originally "ButtonClicked"