简体   繁体   English

iPhone SDK:以编程方式提交UIWebview

[英]iPhone SDK: Submit a UIWebview programmatically

I have a UIWebview that automatically fills in the username and password of the user. 我有一个UIWebview,可以自动填写用户名和密码。 Now, all I need is for the webview to automatically submit as if the user had pressed the Sign in button. 现在,我所需要做的就是让Webview自动提交,就像用户按下“登录”按钮一样。 Is there anyway to call this function programmatically to a webview? 无论如何,是否可以通过编程方式将此功能调用到Webview? By the way, I am using hotmail as the website if this helps at all. 顺便说一句,如果有帮助的话,我正在使用hotmail作为网站。

To cause the form to be submitted, what you need to do is execute a piece of javascript code that finds the DOM element of the submit button, and then calls the click() method on that. 要提交表单,您需要做的是执行一段JavaScript代码,找到提交按钮的DOM元素,然后在其上调用click()方法。

I don't know anything about the structure of the hotmail login page, but if there's an id attribute on the submit button, you can use document.getElementById(...) to retrieve it. 我对hotmail登录页面的结构一无所知,但如果“提交”按钮上有id属性,则可以使用document.getElementById(...)进行检索。 Otherwise you may have to write code to look through all the input elements to find one with type="submit". 否则,您可能必须编写代码以浏览所有输入元素,以找到带有type =“ submit”的元素。

In the simple case, let's suppose the submit button has an id of "submit_button". 在简单的情况下,假设提交按钮的ID为“ submit_button”。 Then in your Objective C code you would have the following: 然后,在目标C代码中将具有以下内容:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *code = @"document.getElementById('submit_button').click()";
    [webView stringByEvaluatingJavaScriptFromString: code]
}

If you need to do anything more complex, you can change the content of the code string. 如果您需要做更复杂的事情,则可以更改代码字符串的内容。 This is simply javascript code that gets run in the context of the page. 这只是在页面上下文中运行的javascript代码。

Stepping back for a moment though, I'd recommend you think about a more reliable way to do this. 不过,退一步,我建议您考虑一种更可靠的方法。 Hotmail's login page could change at any time, and your app will break if they alter the way that the submit button works, eg giving it a different name, or using some other means of detecting input (such as an javascript onclick handler). Hotmail的登录页面可能随时更改,如果您的应用改变了提交按钮的工作方式(例如,给它命名一个不同的名称)或使用其他检测输入的方式(例如javascript onclick处理程序),您的应用就会中断。 In general, relying on a specific HTML structure of a site in your app is likely to cause problems - if you can, you're better off using a web service (eg REST) style API, if one is provided. 通常,依赖于应用程序中网站的特定HTML结构可能会引起问题-如果可以,最好使用Web服务(例如REST)样式的API(如果提供)。

The web content that gets loaded in the web view should include the javascript to submit the page. 在网络视图中加载的网络内容应包含用于提交页面的JavaScript。 Implement webViewDidFinishLoad: in your WebViewController & call stringByEvaluatingJavaScriptFromString:@"yourJavascriptSubmitMethodCall()" I am away from mac/xcode. 在您的WebViewController中实现webViewDidFinishLoad:并调用stringByEvaluatingJavaScriptFromString:@"yourJavascriptSubmitMethodCall()"我离开了mac / xcode。 Worth trying. 值得尝试。

You can search the webview for submit buttons, then click on the first. 您可以在Web视图中搜索提交按钮,然后单击第一个。 It should work in a lot of cases. 它在很多情况下都应该起作用。

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

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

}

Web view is not mean to call functions for you. Web视图并不意味着为您调用函数。 It will just help you to show your page contents in a specified format. 它只会帮助您以指定的格式显示页面内容。 Log in or any other such functionality should be handled programatically at the back end of your web site code. 登录或任何其他此类功能应在网站代码的后端以编程方式进行处理。 That's what I can say on this. 这就是我可以说的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式单击UIWebView中的“提交”按钮(无元素ID,名称,类或标签) - Programmatically clicking 'submit' button in UIWebView (no element id, name, class or tag) 如何为iPhone UIWebView中加载的页面设置默认(提交)按钮(stringByEvaluatingJavaScriptFromString?) - How to set the default (submit) button for a page loaded in an iPhone UIWebView (stringByEvaluatingJavaScriptFromString?) iPhone SDK:如何向添加到UIWebView中的UIButton添加事件 - iphone sdk: how to add event to UIButton added in UIWebView iphone sdk:如何摆脱UIWebView中的操作表? - iphone sdk: How to get rid of the action sheet in UIWebView? 如何在iPhone SDK应用程序中通过Javascript提交表单? - How to submit a form through Javascript in an iPhone SDK application? 我希望我的程序能够检测到用户何时通过uiwebview(iphone)单击网站上的“提交”按钮 - I want my program to detect when the user clicks submit button on a website via uiwebview (iphone) 以编程方式填写UIWebview中的数据 - Programmatically fill in data in a UIWebview 以编程方式在UIWebView中选择文本 - Programmatically select text in UIWebView 以编程方式启动UIWebView键盘 - Programmatically launch UIWebView Keyboard 在UIWebView中以编程方式显示键盘 - Programmatically display keyboard in UIWebView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM