简体   繁体   中英

How to pass swift UITextField value to HTML Form Text in WKWebView Swift 3?

I have this HTML form:

  <p>
    <form>
      <input type="text" id="result" name="Result" width="20"/>
    </form>
  </p>

How do I pass String value from my UITextField in swift to that form?

Use evaluateJavaScript method like below.

class
ViewController: UIViewController {

    @IBOutlet   weak    var oWV: WKWebView!
    @IBOutlet   weak    var oTF: UITextField!
    override func
    viewDidLoad() {
        super.viewDidLoad()
        oWV.loadHTMLString( """
<p>
    <form>
        <input type="text" id="result" name="Result" width="20"/>
    </form>
</p>
"""
        ,   baseURL: nil
        )
    }
    @IBAction func
    Do( _ p: Any ) {
        oWV.evaluateJavaScript(
            "document.querySelector( '#result' ).value='" + ( oTF.text ?? "" ) + "'"
        ) { p, e in
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM