繁体   English   中英

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

[英]Executing curl based actions in Objective-C

我试图在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];
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类参考 ,看看如何设置。

你可以使用NSURLConnection ; 这是一个样本:

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

可能有一种方法可以使用NSTask实际运行curl脚本: httpNSTask

我搜索了很多来转换这个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]);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM