在 WKWebView 中填充输入文本字段 [英] Populating input text field in WKWebView

查看:38
本文介绍了在 WKWebView 中填充输入文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在由特定视图控制器控制的 WKWebView 中加载了以下 html 页面.视图加载后,我希望应用程序自动填充页面中的输入文本字段.

I have loaded the following html page in a WKWebView controlled by a specific view controller. After the view is loaded I would like that the App automatically populates an input text field in the page.

在此示例中,我希望应用在下面表单的结果"文本字段中写入Hello Result".

In this example I would like the app to write "Hello Result" in the "Result" text field of the form below.

<html>
<head>
    <title>Test Page</title>
</head>
<body>
<form>
 QR Result <input type="text" name="Result" width="20">
</form>
</body>
</html>

这是我在视图控制器中加载HTML页面作为文件的代码:

This is the code where I load the HTML page as file within the view controller:

   var htmlFile = NSBundle.mainBundle().pathForResource("apptest", ofType: "html")
        var contents = NSString(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding, error: nil)
        self.webView?.loadHTMLString(contents!, baseURL: nil)

我如何加载视图(文件或 URL)并不重要.我希望能够做的是在加载视图之后的某个时间填充该字段.

It doesn't matter how I load the view (file or URL). What I want to be able to do is to fill the field at some point after loading the view.

推荐答案

Uttam Sinha 的答案对应的 wkwebview/swift 是.

The wkwebview/swift equivalent for Uttam Sinha's answer would be.

    webview.evaluateJavaScript("document.getElementById('result').value = 'Hello Result';", completionHandler: { (res, error) -> Void in
        //Here you can check for results if needed (res) or whether the execution was successful (error)
    })

WKWebView 的版本更好,因为它会在执行 JS 出现问题时提供错误反馈.

The WKWebView's version is nicer since it will provide error feedback in case of problems executing your JS.

这篇关于在 WKWebView 中填充输入文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆