原学程将引见用户实质掌握器从未从JS注进回调的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我碰到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, 四四0, 六00)
}
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桥仿佛便是做没有到这1面。
有人晓得为何吗?
我应用的是OSX 一0.一一.六,运转的是XCode 七.三.六。
总的去说,我对于Swift以及OSX编程真的很生疏,所以请绝不迟疑天指出我漏掉的所有元素。我自愿简略了我的页里应用的JS,由于我在示例中出有应用所有JS。
感谢。
推举谜底
找到了,完整是我的错。
将剧本添减到Webview设置装备摆设时,我忘却重定名新闻处置法式。
userController.addScriptMessageHandler(self, name: "callbackHandler") // was originally "ButtonClicked"
佳了闭于用户实质掌握器从未从JS注进回调的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。