2

I am looking for a starting point on a project that needs to display a UIWebView on an iPad. THe catch is that the HTML will be generated by the pad and displayed in the UIWebView, and will contain many input controls.

What is needed is a way to grab the contents of these controls after the user has completed entry similar to how I would do it on a server. I need to grab this entered data on the iPad without an actual submit.

Does anyone know the starting point for this type of interaction?

Rob Bonner
  • 9,276
  • 8
  • 35
  • 55
  • I also have this question. HTML is a great and easy language for creating forms. I need to define forms for use on the iPad, but I want my iPad app to process the form. Much easier than defining my own format and creating a custom view. – David Feb 21 '12 at 19:31

4 Answers4

2

You can do this by implementing the UIWebViewDelegate delegate's shouldStartLoadWithRequest method:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSData* data = request.HTTPBody;
    NSString* s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if([s length] == 0)
        return YES;
    else
        return NO;
}

It works fine with a post.

d512
  • 32,267
  • 28
  • 81
  • 107
0

It can be done in simple way.. we know HTTP request contains -

  1. Method (GET,POST..etc)
  2. HTTP header
  3. HTTP body

we can check header field value for Conent-type if it is x-www-form-urlencoded then form field values are sending thru them as key=value pairs

then we can catch therse paires in webView:shouldStartLoadWithRequest:navigationType: - in request parameter as

[request HTTPBody], similarly we can get method [HTTPMethod]..etc

if it is simply GET method then all pairs will be in request itself.

:) hope it helps

jnix
  • 480
  • 3
  • 10
0

Here's a way to do it:

  1. Register a custom URL scheme for your App (see here f.e. http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html)
  2. When the user touches your save/submit/whatever button you read out the values of all needed form-fields, construct a url that matches your URL scheme and redirect to this URL with JavaScript (window.location) and work with the data in Objective-C and do what you have to do.

Example URL could be: myapp://value_of_field1/value_of_field2/...

See the linked tutorial on how to register a custom scheme and how to retrieve the data in Obj-C.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57
  • Great answer. The part I am confused on is the read out the values of all needed form-fields. I am thinking that trough a link in the form I can gain control of the press, but if the user has entered data, would it then be in the HTML of the form? – Rob Bonner Feb 10 '11 at 18:50
  • Yes, if the user enters something into an input field, you can read it out by getting the fields value attribute. Example: var firstValue = document.getElementById("myFormField").value – Björn Kaiser Feb 11 '11 at 13:33
0

Within the previously posted article it also mentioned the UIWebViewDelegate method,

        webView:shouldStartLoadWithRequest:navigationType:

This gets invoked on the delegate before a link is followed. I haven't tried it, but this method might be invoked when submitting the form. Use a GET method. Easier than having to loop out of the app and back.

David
  • 2,770
  • 5
  • 35
  • 43