2011-05-13 43 views
2

我想实现在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]; 

回答

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看看怎么设置。

+5

正是。同样使用'sendSynchronousRequest:returningResponse:'很少是一个好主意。如果你最后想要一个可用的应用程序,你应该使用异步方法调用'NSURLConnection'。 – toto 2011-05-13 12:43:59

+0

@toto:+1编辑了我的答案。但总是取决于请求时间以及您的应用会是什么样子,即,如果它能够在没有响应的情况下工作。 – 2011-05-13 12:45:00

+1

@toto:我会说在受控后台线程中使用'sendSynchronousRequest:returningResponse:'(从不主线程)*实际上会导致更容易测试,维护和扩展的代码。 – PeyloW 2011-05-13 14:59:28

0

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

}