Home  >  Q&A  >  body text

Using JavaScript function calls

<p>I want to call a JS function from Swift</p> <p>I want to get the result of JS in Swift. I have a lot of functions that need to be called from Swift. These functions may or may not contain parameters. You also need to include a closure for the result. </p>
P粉118698740P粉118698740432 days ago482

reply all(1)I'll reply

  • P粉493534105

    P粉4935341052023-08-16 00:22:29

    Call the function like this:

    func setCompleteAdInJavaScript() {
        let javascriptFunction = """
        gameState.setCredits('200');
        """
        
        webView.evaluateJavaScript(javascriptFunction) { result, error in
            
            if let _ = error {
                print("设置积分出错")
                
            } else {
                print("成功设置积分。")
                
            }
            
        }
        
    }

    reply
    0
  • Cancelreply