I need my swift class to interact with the html and javascript in a wkwebview that it references, in particular, feed it a variable.
I thought I would start simply by trying to get the webview to fire an alert:
Here is the code:
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
webView.navigationDelegate = self as? WKNavigationDelegate
if let url = Bundle.main.url(forResource: "tradingview", withExtension: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}
// Try one way in viewdidload. Compiles but doesn't do anything
webView.evaluateJavaScript("alert('hello from the webview');");
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
//try another way. Also doesn't do anything
webView.evaluateJavaScript("alert('hello from webview')"), completionHandler: nil)
}
override func loadView() {
self.view = webView
}
However, the webview is not firing an alert. What is wrong with the code or is there anything else you need to do to get Swift to run some javascript on a webview.
Thanks for any suggestions.