Objective-c – Grabbing POST data from UIWebView

objective c

I was wondering if it were possible to grab POST data and save it when a submit button was pressed in a UIWebView. Should I be using javascript and adding eventlisteners so that I can get the values before the submit goes through? If so, how would I be able to get the data back to my code in obj c? Otherwise, is there any easier way? Thanks

Best Answer

You need to implement the UIWebViewDelegate. There is a hook called "shouldStartLoadWithRequest" that gets called by iOS.

Psueudocode below.

@interface MyController<UIWebViewDelegate>
@end

@implementation MyController
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request       navigationType:(UIWebViewNavigationType)navigationType
{
    // do your magic
    NSData *data = request.HTTPBody; // contains the HTTP body as in an HTTP POST request.

    // return YES to continue to load the URL
}
@end
Related Topic