UIWebView capture post

两盒软妹~` 提交于 2019-12-10 10:59:24

问题


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?


回答1:


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.




回答2:


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:


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




回答4:


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.



来源:https://stackoverflow.com/questions/4958762/uiwebview-capture-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!