UIWebView capture post

2019-05-23 22:35发布

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?

4条回答
Summer. ? 凉城
2楼-- · 2019-05-23 23:14

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.

查看更多
够拽才男人
3楼-- · 2019-05-23 23:19

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.

查看更多
Fickle 薄情
4楼-- · 2019-05-23 23:22

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

查看更多
小情绪 Triste *
5楼-- · 2019-05-23 23:32

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.

查看更多
登录 后发表回答