我正在尝试在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有效负载并将其POST到服务器(我更喜欢这是异步请求而不是同步)。
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];
答案 0 :(得分:7)
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,了解如何设置内容。
答案 1 :(得分:1)
您可以使用NSURLConnection
;这是一个样本:
How do I make HTTP post request for getting JSON object in response for iPhone application?
使用NSTask
可能有办法实际运行卷曲脚本:http://borkware.com/quickies/one?topic=nstask
答案 2 :(得分:0)
我搜索了很多来转换这个cUrl请求以发送Urban Airship推送通知,我最后在下面做的是对我有用的源代码:
的必需:强>
ASIHTTPRequest //执行Http请求所必需的
+(void)sendUrbanAirshipPushWithPayload:(NSString *) payload
{
//payload = @"{"aps": {"badge": 1, "alert": "The quick brown fox jumps over the lazy dog."}, "aliases": ["12345"]}";
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setRequestMethod:@"POST"];
//Set the HTTP Basic Authentication header with the application key as the username, and the master secret for the password
[request setUsername:kUrbanAirshipKey];
[request setPassword:kUrbanAirshipMasterSecret];
[request setPostBody:[NSMutableData dataWithData:[payload dataUsingEncoding:NSUTF8StringEncoding]]];
[request startSynchronous];
if([request error])
{
NSLog(@"Code : %d, Error: %@",[[request error] code],[[request error] description]);
}
NSLog(@"Code: %d, Description: %@, Message: %@",[request responseStatusCode],[request responseData],[request responseStatusMessage]);
}