用户内容控制器从未从JS注入回调
我遇到WBWebview
遇到NSPopover
。
我遵循this指南,以便能够将数据从JS发送回我的SWIFT应用程序。
遗憾的是,SWIFT应用程序从未调用过userContentController
。
以下是我的SWIFT应用程序查看控制器代码:
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()
}
}
我正在尝试直接注入对SWIFT回调的调用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,因为我在示例中没有使用任何JS。
谢谢。
解决方案
找到了,完全是我的错。 将脚本添加到Webview配置时,我忘记重命名消息处理程序。
userController.addScriptMessageHandler(self, name: "callbackHandler") // was originally "ButtonClicked"
相关文章