通过POST而不是GET将数据传递到目标c [英] Passing data to objective c with POST rather than GET
问题描述
我一直在使用url拦截方法,通过将数据作为url编码的参数传递并使用NSURLProtocol来拦截数据,从而将数据从javascript传递到目标C,但是我现在想发送更大量的数据,例如说10,000个字符长字符串,但这在GET请求中似乎不切实际.对吧?
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?
目标c是否有办法拦截从UIWebView发送的POST数据?
如果是这样,我仍然使用NSURLProtocol以及如何获取POST数据吗?
如果没有其他方法,我可以将大量数据从UIWebView传递到目标c?
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?
推荐答案
在使用类似以下代码时:
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
对某些协议(例如app://
)的请求将导致[request HTTPBody]
为null
.但是,如果通过http://
发送,则[request HTTPBody]
将按预期将请求数据保存在NSData
对象中.
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.
所以您的Javascript应该类似于:
So your Javascript should look something like:
$.post("http://test/hello/world", {'data':"foo bar"});
并且不类似:
$.post("app://test/hello/world", {'data':"foo bar"});
这篇关于通过POST而不是GET将数据传递到目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!