在 UIWebView 中提交表单

发布于 2024-12-05 09:32:12 字数 334 浏览 0 评论 0原文

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


更新:

有没有办法将网页视图中的表单数据保存回我的程序?我认为当我单击“提交”按钮时,最好将表单数据保存回程序(即将参数存储到 nsstring 中),然后开始查询 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!


Update:

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.

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

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

发布评论

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

评论(2

毁梦 2024-12-12 09:32:12

您可以使用该

stringByEvaluatingJavaScriptFromString

函数从目标 c 触发 javascript 方法并从该方法获取返回值。

例如

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

,在 javascript 中

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

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

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

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

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

    return YES;
}

You can use the

stringByEvaluatingJavaScriptFromString

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

For example

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

and in javascript

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

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.

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;
}
我恋#小黄人 2024-12-12 09:32:12

如果我正确理解您的问题,我认为以下步骤将帮助您提供解决方案。

  1. 在表单的操作中,您需要写入Web服务的URL。
  2. 在您的表单中,输入字段的名称应与 Web 服务所需的参数名称相匹配。
  3. 在您的第一个 UIWebView 导航控件中,创建具有第二个 UIWebView 的屏幕对象,并将 Web 服务的响应设置为该对象的实例成员。
  4. 在你的第一个 UIWebView 导航控件中,你需要编写
    [selfpresentModalViewController:webView2Objectanimated:YES];
  5. 在有 webView2 的屏幕中,在加载第二个 UIWebView 之前解析响应。并使用 javascript 将其注入显示的页面。

If I understood your problem correctly, I think following steps will help you to provide the solution.

  1. In the action of form, you need to write the URL to the web service.
  2. In your form, the name of the input field should be matched with the name of the parameters expected for the web service.
  3. In your first UIWebView Navigation control, Create the object of screen having second UIWebView and set the response of the web service to the instance member of the object.
  4. In your first UIWebView Navigation control, you need to write
    [self presentModalViewController:webView2Object animated:YES];
  5. In the screen of having webView2, parse the response before loading the second UIWebView. and inject it with the displayed page using javascript.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文