userContentController 从未从 JS 注入中回调
userContentController never called back from JS injection
我已经把 WBWebview
运行 变成了 NSPopover
.
我遵循了 this 指南,以便能够将数据从 JS 发送回我的 Swift 应用程序。
不幸的是,userContentController
从未在 Swift 应用程序中被调用。
这是我的 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 方式工作(我已经能够修改可见 HTML 元素的 CSS通过注入 JQuery 代码),但是 JS -> Swift 桥似乎没有成功。
有人知道为什么吗?
我在 OSX 10.11.6,运行 XCode 7.3.6.
总体而言,我对 Swift 和 OSX 编程是个新手,所以请毫不犹豫地指出我遗漏的任何元素。我自愿省略我的页面使用的 JS,因为我没有在示例中使用任何它。
谢谢。
找到了,完全是我的错。
我在将脚本添加到 webview 配置时忘记重命名消息处理程序。
userController.addScriptMessageHandler(self, name: "callbackHandler") // was originally "ButtonClicked"
我已经把 WBWebview
运行 变成了 NSPopover
.
我遵循了 this 指南,以便能够将数据从 JS 发送回我的 Swift 应用程序。
不幸的是,userContentController
从未在 Swift 应用程序中被调用。
这是我的 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 方式工作(我已经能够修改可见 HTML 元素的 CSS通过注入 JQuery 代码),但是 JS -> Swift 桥似乎没有成功。 有人知道为什么吗?
我在 OSX 10.11.6,运行 XCode 7.3.6.
总体而言,我对 Swift 和 OSX 编程是个新手,所以请毫不犹豫地指出我遗漏的任何元素。我自愿省略我的页面使用的 JS,因为我没有在示例中使用任何它。
谢谢。
找到了,完全是我的错。 我在将脚本添加到 webview 配置时忘记重命名消息处理程序。
userController.addScriptMessageHandler(self, name: "callbackHandler") // was originally "ButtonClicked"