开发者

UIWebView capture post

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 the开发者_如何学Pythonse 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?


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜