在 UIWebView 中提交表单

Submit a form in UIWebView(在 UIWebView 中提交表单)

本文介绍了在 UIWebView 中提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIWebView 中有一个与 Web 服务交互的表单,当我提交表单时,数据被发送到 Web 服务,并将结果返回到另一个 UIWebView.这两个 UIWebView 都在一个导航控制器下,所以当用户提交表单时,它会滑动到另一个视图来显示结果.谁能指出我如何做到这一点?我在表单元素中填写action"属性是什么?如何将用户输入返回到应用程序,以便我可以使用 Web 服务?我是这个领域的新手,任何帮助将不胜感激.谢谢!

I have a form in the UIWebView to interact with a web service, when I submit the form, the data gets sent to a web service and it will return the result to another UIWebView. The two UIWebViews are under a navigation controller, so when the use submits the form, it slides to another view to display the result. Can anyone point me out how I can achieve this? What do I fill in the "action" attribute in the form element? How can I get the user input back to the application so I can use the web service? I am new to this field, any help will be appreciated. Thanks!

更新:

有没有办法将表单数据从 Web 视图保存回我的程序?我认为当我单击提交按钮时,最好将表单数据保存回程序(即将参数存储到 nsstring),然后开始查询 Web 服务.

Is there a way to save form data from web view back to my program? I think it's better to save form data back to the program (i.e. store params into nsstring) when I click submit button, and then start querying web service.

推荐答案

你可以使用

stringByEvaluatingJavaScriptFromString

用于从目标 c 触发 javascript 方法并从该方法获取返回值的函数.

function for firing a javascript method from objective c and get a return value from that method.

例如

//Calling the getTheUsername in the javascript.
NSString *userName = [yourWebView stringByEvaluatingJavaScriptFromString:@"getTheUsername()"];

在javascript中

and in javascript

//Method in javascript
function getTheUsername()
{
return userNameVariable;
}

我怀疑在这里你可能想做反之亦然(从 javascript 调用 obj-c 方法).这不能直接完成,这是解决方法.

And I doubt that here you may wanna do vice-versa (Call obj-c method from javascript). This cannot be done directly here is the work around.

设置一个UIWebViewDelegate,实现webView:shouldStartLoadWithRequest:navigationType:方法.在您的 JavaScript 代码中,导航到一些虚假 URL,该 URL 对您要传递给应用程序的信息进行编码,例如:window.location = "someLink://yourApp/form_Submitted:param1:param2:param3";在您的委托方法中,查找这些虚假 URL,提取您需要的信息,采取任何适当的操作,然后返回 NO 以取消导航.

Set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say: window.location = "someLink://yourApp/form_Submitted:param1:param2:param3"; In your delegate method, look for these fake URLs, extract the information you need, take whatever action is appropriate, and return NO to cancel the navigation.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSLog(@"%@",[[request URL] query]);

    return YES;
}

这篇关于在 UIWebView 中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UIWebView 中提交表单

基础教程推荐