我正在尝试在Objective-C中实现以下目标:

curl -X POST -u "<application key>:<master secret>" \
    -H "Content-Type: application/json" \
    --data '{"aps": {"badge": 1, "alert": "The quick brown fox jumps over the lazy dog."}, "aliases": ["12345"]}' \
    https://go.urbanairship.com/api/push/

我可以使用某种实现此目的的库吗?显然,我已经准备好了所有值,可以继续提出要求,但是我不确定在Objective-C中如何实现。

我正在使用TouchJSON,但是我不确定如何在上面构造正确的JSON有效负载并将其发布到服务器(也希望将其作为异步请求而不是同步请求)。
NSError *theError = NULL;

NSArray *keys = [NSArray arrayWithObjects:@"aps", @"badge", @"alert", @"aliases", nil];
NSArray *objects = [NSArray arrayWithObjects:?, ?, ?, ?, nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *theURL = [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *theBodyData = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary error:&theError];
[theRequest setHTTPBody:theBodyData];

NSURLResponse *theResponse = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseData error:&theError];

最佳答案

NSURL *url = [NSURL URLWithString:@"https://go.urbanairship.com/api/push/"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
//... set everything else
NSData *res = [NSURLConnection  sendSynchronousRequest:req returningResponse:NULL error:NULL];

或发送异步请求
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:some];

看看NSMutableURLRequest Class Reference看看如何设置。

10-06 15:53