I am trying to pass data from my sites Javascript to my app, written in Swift.
Javascript:
var messgeToPost = {
"username": "testing123"
};
window.webkit.messageHandlers.loginSuccess.postMessage(messgeToPost);
Swift:
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)
}
}
Unfortunately, print(password)
is never called.
What am I missing here?
var messgeToPost = {
"username": "testing123",
"password": "password"
};
forgot to add password
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With