When I try the code below on Playground, res is always nil. I tried different Javascript commands. Any ideas why?
Code Block ` swift import Cocoa import WebKit let url = URL(string: "SOMEURL")! let config = WKWebViewConfiguration() let prefs = WKPreferences() prefs.javaScriptEnabled = true config.preferences = prefs let webView = WKWebView(frame: .zero, configuration: config) let navDel = NavDel() webView.navigationDelegate = navDel webView.load(URLRequest(url: url)) class NavDel: NSObject, WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { DispatchQueue.main.async { let js = "document.documentElement.textContent" webView.evaluateJavaScript(js) { (res, error) in guard error != nil else { print(error?.localizedDescription) return } print(res) // returns nil } } } }
`