使用POST而不是GET将数据传递到目标c

yod*_*een 3 javascript objective-c uiwebview nsurlprotocol

我一直在使用url拦截方法将数据从javascript传递到目标C,方法是将数据作为url编码参数传递,并使用NSURLProtocol来拦截请求但是我现在想要发送更大量的数据,比如10,000个字符长的字符串,但是这个在GET请求中似乎不切实际.对?

有没有办法让目标c拦截从UIWebView发送的POST数据?
如果是这样,我仍然使用NSURLProtocol,如何获取POST数据?
如果没有,我可以通过其他方式将大量数据从UIWebView传递到目标c吗?

Tra*_*vis 5

使用代码时:

@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

对某些协议(例如app://)的请求将导致[request HTTPBody]存在null.但是,如果您通过发送,http://那么[request HTTPBody]将按NSData预期在对象中包含请求数据.

所以你的Javascript应该类似于:

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

不是像:

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