在Objective-C中执行基于curl的操作

我正在尝试在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,但是我不太确定如何构build正确的JSON负载,并将其发布到服务器(也是我更喜欢这是一个asynchronous请求,而不是同步)。

 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]; 

或者发送asynchronous请求

 NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:some]; 

看看NSMutableURLRequest类参考 ,看看如何设置什么。

你可以为此使用NSURLConnection ; 这里是一个示例:

如何使HTTP请求获取响应iPhone应用程序的JSON对象?

可能有一种方法可以使用NSTask真正运行curl脚本: http : NSTask

我search了很多转换这个请求发送城市飞艇推送通知,我终于做到了下面是我的工作源代码:
必需: 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]); }