8

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.

Michael
  • 2,075
  • 7
  • 32
  • 46
  • Is your Submit button a native UIButton or a HTML Button element. – sElanthiraiyan Sep 19 '11 at 07:18
  • Its a html "submit" button, that's why I wonder how to save data to my program. – Michael Sep 19 '11 at 07:45
  • then you should use the second work around that i mentioned in my answer. Navigate to a fake url in the submit button and shouldStartLoadWithRequest will be fired. Inside that method call a javascript method using stringByEvaluatingJavaScriptFromString. In that javascript method return the required data. Got it! – sElanthiraiyan Sep 19 '11 at 08:23
  • I looked at your solution and that seems a bit dizzy for me. What if I set a button outside the web view and use javascript to return the field values to the app? – Michael Sep 19 '11 at 09:42
  • OK If you have a submit button in native, add a javascript method which will return your required data. Call that method using stringByEvaluatingJavaScriptFromString and get the data from there. – sElanthiraiyan Sep 19 '11 at 09:49
  • And one more question, where do I put the javascript function? In my html file? How do my web view know where to find this function? – Michael Sep 19 '11 at 10:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3583/discussion-between-michael-and-elanthiraiyans) – Michael Sep 19 '11 at 10:03

2 Answers2

11

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;
}
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
0

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.
Naved
  • 4,020
  • 4
  • 31
  • 45
  • 1
    Does that mean I have to put the request in the form? As the entire form is in the uiwebview, how can I get the result back to a uiview (like a list in a table view)? – Michael Sep 19 '11 at 06:36
  • If you keep the name of the input elements in the form same as the name of parameters for your Web Service URL, the parameter will automatically will set with your URL and will be submitted to that URL. It will then start loading new page, you can check it inside webViewDidStartLoad delegate method. – Naved Sep 19 '11 at 07:50