我正在尝试将数据从我的网站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)
。
我在这里缺少什么?
答案 0 :(得分:4)
var messgeToPost = {
"username": "testing123",
"password": "password"
};
忘了添加密码