iosswiftwkwebviewevaluatejavascript

How to run javascript in a webview from Swift


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.


Solution

  • You need to convert info in javascript alert into native UIAlert.

    Add alert handler delegate described in WKUIDelegate.

    func webView(_ webView: WKWebView,
                 runJavaScriptAlertPanelWithMessage message: String,
                 initiatedByFrame frame: WKFrameInfo,
                 completionHandler: @escaping () -> Void) {
    
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let title = NSLocalizedString("OK", comment: "OK Button")
        let ok = UIAlertAction(title: title, style: .default) { (action: UIAlertAction) -> Void in
            alert.dismiss(animated: true, completion: nil)
        }
        alert.addAction(ok)
        present(alert, animated: true)
        completionHandler()
    }
    

    And call like below (there is a type in your code);

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("alert('hello from the webview')")
    }
    

    enter image description here


    In Addition

    There is a sample project that simulates two way communication between native and web in both way.