在UIWebView中提交表单 [英] Submit a form in UIWebView

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

问题描述

我在UIWebView中有一个表单与W​​eb服务交互,当我提交表单时,数据被发送到Web服务,它会将结果返回给另一个UIWebView。两个UIWebViews位于导航控制器下,因此当使用提交表单时,它会滑动到另一个视图以显示结果。任何人都可以指出我如何实现这一目标?我在表单元素中填写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视图回到我的程序的数据?我认为最好将表单数据保存回程序(即将params存入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;
}

我怀疑你在这里反之亦然(请致电obj-来自javascript的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中提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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