我想实现在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];
正是。同样使用'sendSynchronousRequest:returningResponse:'很少是一个好主意。如果你最后想要一个可用的应用程序,你应该使用异步方法调用'NSURLConnection'。 – toto 2011-05-13 12:43:59
@toto:+1编辑了我的答案。但总是取决于请求时间以及您的应用会是什么样子,即,如果它能够在没有响应的情况下工作。 – 2011-05-13 12:45:00
@toto:我会说在受控后台线程中使用'sendSynchronousRequest:returningResponse:'(从不主线程)*实际上会导致更容易测试,维护和扩展的代码。 – PeyloW 2011-05-13 14:59:28