如何将数据从Javascript传递到WKWebView中的Swift?
我试图将数据从我的网站JavaScript传递到我的应用程序,用Swift编写。
使用Javascript:
var messgeToPost = {
"username": "testing123"
};
window.webkit.messageHandlers.loginSuccess.postMessage(messgeToPost);
迅速:
var webConfig:WKWebViewConfiguration {
get {
var webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
var userController:WKUserContentController = WKUserContentController()
userController.addScriptMessageHandler(self, name: "loginSuccess")
webCfg.userContentController = userController;
return webCfg;
}
}
override func viewDidLoad() {
var userController:WKUserContentController = WKUserContentController()
userController.addScriptMessageHandler(self, name: "loginSuccess")
webView = WKWebView(frame: self.view.frame,
configuration: webConfig)
webView.loadRequest(NSURLRequest(URL: NSURL(string: URLPath)!))
}
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
if let messageBody:NSDictionary = message.body as? NSDictionary {
let password:String = messageBody["password"] as! String
print(password)
}
}
不幸的是, print(password)
从未被调用过。
我在这里错过了什么?
var messgeToPost = {
"username": "testing123",
"password": "password"
};
忘了添加密码
链接地址: http://www.djcxy.com/p/90403.html上一篇: How to pass data from Javascript to Swift within a WKWebView?
下一篇: How to output a massive file with PHP without running out of memory