自动填写并提交 HTML 表单并在 UIWebView iOS 中查看

发布于 2024-12-05 03:53:07 字数 329 浏览 1 评论 0原文

我试图在 iPhone 和 iPad 上自动填写并提交 HTML 表单,然后在 UIWebView 中查看结果页面。

示例:有一个网站可以为我的大学的膳食计划充值,它要求您先输入您的学号,按提交,输入您的密码,按提交,然后它会显示您的余额。

我正在尝试制作一个应用程序,让您在其中保存信息,按“GO”,然后在后台它将填写保存的学号,点击“提交”,等待加载结果页面,然后输入密码,按提交,然后最终显示带有余额的结果页面。

我已多次尝试提交表单,但无法让它们显示在 UIWebView 中。

有人有任何想法或解决方案吗?

感谢所有的帮助!抱歉,如果这是一个愚蠢的问题。

I'm trying to automatically fill and submit an HTML form on the iPhone and iPad, but then view the resulting page in a UIWebView.

Example: There's a website to add money to a meal plan for my university, and it requires you to put in your student number first, press submit, put in your password, press submit, then it reveals your balance after that.

I am trying to make an app that lets you save your information in it, press GO and then in the background it will fill in the saved student number, hit submit, wait for the resulting page to be loaded, then input the password, press submit, then finally display the resulting page with the balance.

I've tried multiple times to submit forms, but can't get them to display in a UIWebView after.

Does anyone have any ideas or solutions?

Appreciate all the help! Sorry if it's a silly question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-12-12 03:53:07

您可以使用 JavaScript 来完成。使用 UIWebView 的 -stringByEvaluatingJavaScriptFromString: 方法输入文本并单击按钮。

You can do it with JavaScript. Use the -stringByEvaluatingJavaScriptFromString: method of UIWebView to enter the text and click the button.

巡山小妖精 2024-12-12 03:53:07

对于那些需要代码的人,我希望这 3 种方法可以有所帮助:

此方法将填写用户名字段:

- (void)completeUserFieldsForWebView:(UIWebView *)webView withUsername:(NSString *)username {

    NSString *loadUsernameJS =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='email']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];
    NSString *loadText =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadText];

}

此方法将填写密码字段:

- (void)completePasswordFieldsForWebView:(UIWebView *)webView withPassword:(NSString *)password {

    NSString *loadPasswordJS =
    [NSString stringWithFormat:@"var passFields = document.querySelectorAll(\"input[type='password']\"); \
     for (var i = passFields.length>>> 0; i--;) { passFields[i].value ='%@';}", password];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];

}

提交表单:

- (void)clickOnSubmitButtonForWebView:(UIWebView *)webView {

    NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
    passFields[0].click()";
    [webView stringByEvaluatingJavaScriptFromString:performSubmitJS];

} 

For those who need the code I hope this 3 methods can help :

This method will fill the username field:

- (void)completeUserFieldsForWebView:(UIWebView *)webView withUsername:(NSString *)username {

    NSString *loadUsernameJS =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='email']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];
    NSString *loadText =
    [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
     for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", username];
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadText];

}

This one will fill the password field:

- (void)completePasswordFieldsForWebView:(UIWebView *)webView withPassword:(NSString *)password {

    NSString *loadPasswordJS =
    [NSString stringWithFormat:@"var passFields = document.querySelectorAll(\"input[type='password']\"); \
     for (var i = passFields.length>>> 0; i--;) { passFields[i].value ='%@';}", password];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];

}

To submit your forms :

- (void)clickOnSubmitButtonForWebView:(UIWebView *)webView {

    NSString *performSubmitJS = @"var passFields = document.querySelectorAll(\"input[type='submit']\"); \
    passFields[0].click()";
    [webView stringByEvaluatingJavaScriptFromString:performSubmitJS];

} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文