SWIFT:填充HTML输入[type ="text"] [英] SWIFT: Filling HTML input [type=“text”]

查看:88
本文介绍了SWIFT:填充HTML输入[type ="text"]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何填写当前在Web视图中显示的HTML表单上的字段? 此应用程序的目的是为了自动登录网站. 根据不同的教程,我主要复制粘贴了一个快速代码,但是我的代码不起作用.很好地提示错误可能是什么,或者非常感谢完全不同的方法.

How to fill fields on an HTML form that is currently being presented in a web view? The purpose of this application is meant to auto log-in into a website. On basis of different tutorials I mostly copy-pasted a swift code but mine does not work. It would be great to get a hint what the mistake could be or even a complete different approach is very much appreciated.

我要登录的网站的重要部分是这样构建的:

The important parts of the website I want to login is built like this:

文本字段用户名:

<input type="text" class="form-control" name="username" id="username" placeholder="Username:">

文本字段密码:

<input type="password" class="form-control" name="password" id="password" maxlength="20" placeholder="Passwort:">

登录按钮(提交登录):

<input type="submit" value="login" class="btn btn-default">

我破碎的 ViewController.swift 的swift代码:

My broken swift-code of the ViewController.swift:

它只是加载网站,但不填写用户名或密码,而无需提交.

It just loads the website but without filling in the username or password and without submitting.

@IBOutlet weak var MyWebView: UIWebView!

func webViewDidFinishLoad(MyWebView: UIWebView) {

    // fill data
    let savedUsername = "USERNAME"
    let savedPassword = "PASSWORD"

    let fillForm = String(format: "document.getElementById('username').value = '\(savedUsername)';document.getElementById('password').value = '\(savedPassword)';")
    MyWebView.stringByEvaluatingJavaScript(from: fillForm)

    //submit form
    let deadlineTime = DispatchTime.now() + .seconds(1)
    DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
        MyWebView.stringByEvaluatingJavaScript(from: "document.value[\"login\"].submit();")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL (string: "https://www.austrocontrol.at/flugwetter/")
    MyWebView.loadRequest(URLRequest(url: url!))
    webViewDidFinishLoad(MyWebView: MyWebView)
}

我不确定如何处理从Xcode模拟器获得的输出,但是也许可以通过以下方式有所帮助:

I am not sure how to deal with this output I got from the Xcode-simulator but maybe it helps in a way:

[...] Starting WebFilter logging for process AustroControl Viewer
[...] _userSettingsForUser : (null)
[...] _WebFilterIsActive returning: NO
[...] [MediaRemote] [AVOutputContext] WARNING: AVF context unavailable for +[MRAVOutputContext sharedAudioPresentationContext]_block_invoke
[...] [MediaRemote] [AVOutputContext] WARNING: AVF context unavailable for +[MRAVOutputContext createOutputContextWithUniqueIdentifier:]

推荐答案

首先:最好使用WKWebView.

您可以创建一个在WebView中运行JavaScript并在提交HTML表单之前填写它的​​函数.

You can make a function that runs JavaScript in your WebView and fills the HTML form just before submitting it.

private func setWebViewValue(name: String, data: String) {
    let jsFunc = "document.getElementById(\"\(name)\").value = \"\(data)\""
    webView.evaluateJavaScript(jsFunc, completionHandler: nil)
}

这篇关于SWIFT:填充HTML输入[type ="text"]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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