3

I have been using the url intercept method to pass data from javascript to objective C by passing the data as url encoded parameters and using NSURLProtocol to intercept the request however I am now wanting to send larger amounts of data like say 10,000 character long strings but this does not seem practical to do in a GET request. Right?

Is there a way for objective c to intercept POST data sent from a UIWebView?
If so do I still use NSURLProtocol and how do I get the POST data?
If not is there some other way I can pass larger amounts of data from the UIWebView to objective c?

yodaisgreen
  • 2,310
  • 3
  • 22
  • 27

3 Answers3

5

When using code like:

@implementation AppProtocolHandler

+ (void)registerSpecialProtocol {
    static BOOL inited = NO;

    if (!inited) {
        inited = YES;
        [NSURLProtocol registerClass:[AppProtocolHandler class]];
    }
}

- (void)handleRequest {
    NSURLRequest *request = [self request];

    // null when via app:// but works when via http://
    NSLog(@"[request HTTPBody]: %@", [request HTTPBody]);
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    return YES;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    return request;
}

@end

Requests to some protocols (e.g. app://) will result in [request HTTPBody] being null. But if you send through http:// then the [request HTTPBody] will have the request data in an NSData object as expected.

So your Javascript should look something like:

$.post("http://test/hello/world", {'data':"foo bar"});

And not something like:

$.post("app://test/hello/world", {'data':"foo bar"});
Travis
  • 1,926
  • 1
  • 19
  • 26
  • That was my problem. When I switched to http:// I was able to get the post data. Thanks! – yodaisgreen Oct 24 '10 at 22:48
  • wow, amazing. Is there actually a place where this is documented or you just discovered it by trial and error? This is really difficult to track down – Radu Simionescu Nov 22 '13 at 11:17
  • It's been a long time since I've worked with this, but from what I remember, it was a lot of trial and error. – Travis May 20 '14 at 08:42
1

Any request will be intercepted by the delegate so you can send any POST Ajax request, stuff it with the parameters and values that you want and then send it. All the values will be intercepted and you can use them the same way you are doing so far. An easy POST request could be sent using JQuery, as easy as:

$.post("toobjc.html", { 'data':"10k characters long string and more here ..." });

More here: http://api.jquery.com/jQuery.post/

haknick
  • 1,892
  • 1
  • 20
  • 28
  • I get how to make a post request. My question is can objective c intercept the post data like it intercepts the post request using NSURLProtocol. – yodaisgreen Oct 23 '10 at 20:23
  • 1
    It can intercept the post data and they'll be available in: - "(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSData *data = [request HTTPBody]; . . . }" - in the data variable. – haknick Oct 24 '10 at 00:27
0

You should definitely use a POST. You just need to set up the request for it. You may need to make sure that the data is encoded and take care of a couple other details.:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:myMimeType forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", requestData.length]       
         forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:requestData];

[self.playerView loadRequest: request];

Alternatively, you can send a multi-part document or form values.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • That would send a POST from objective C. What I'm looking for is a way for objective C to intercept a POST and POST data created within a UIWebView. Thanks. – yodaisgreen Oct 23 '10 at 22:33