发送curl from objective-c Xcode

问题描述:

如何使用Xcode发送cURL?发送curl from objective-c Xcode

这是将Shell代码发送到REST Api Server。 将外壳转换为Objective-C代码

curl --include \ 
    --request POST \ 
    --header "Content-Type: application/json; charset=utf-8" \ 
    --header "Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj" \ 
    --data-binary "{\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\", 
\"contents\": {\"en\": \"English Message\"}, 
\"included_segments\": [\"Active Users\"]}" \ 
    https://onesignal.com/api/v1/notifications 

这里有什么问题? 的jsonData输入的问题我

NSURL *url = [NSURL URLWithString:@"https://onesignal.com/api/v1/notifications"]; 
     NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:url]; 
     [rq setHTTPMethod:@"POST"]; 

     NSData *jsonData = [@"{\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\", 
\"contents\": {\"en\": \"English Message\"}, 
\"included_segments\": [\"Active Users\"]}" \" dataUsingEncoding:NSUTF8StringEncoding]; 
     [rq setHTTPBody:jsonData]; 

     [rq setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     [rq setValue:@"Basic O645372ZjItM2NiOC00ZjQ2LTk4Y2UtYjFlMjE5ODBiYzg2" forHTTPHeaderField:@"Authorization"]; 
     [rq setValue:[NSString stringWithFormat:@"%ld", (long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; 

     [NSURLConnection sendAsynchronousRequest:rq completion:^(NSURLResponse *rsp, NSData *data, NSError *err) { 
      NSLog(@"POST sent!"); 
     }]; 

它也可以在邮件中包含一个NSString(NSDictionary中)

NSString *message = @"Test Message 123"; 

NSDictionary *postBody = @{ 
           @"app_id":@"5eb5a37e-b458-11e3-ac11-000c2940e62c", 
           @"contents": @{@"en":@"%@", message}, 
           @"included_segments": @[@"All"] 
          }; 
+0

它看起来并不像你的问题有什么关系iOS或Objective-C的开始与没有更多的信息。此外,你可能不想在任何地方发送cURL。 –

+0

那么你的错误信息是什么?尝试连接之前,您是否在Info.plist中配置了App Transport Security值? –

+0

你好,应用程序配置是好的,我的问题是Json刺激代码(jsonData) –

我的回答是

NSDictionary *postBody = @{ 
           @"app_id":@"5eb5a37e-b458-11e3-ac11-000c2940e62c", 
           @"contents": @{@"en":@"English Message"}, 
           @"included_segments": @[@"All"] 
          }; 

NSData *data = [NSJSONSerialization dataWithJSONObject:postBody options:0 error:nil]; 
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://onesignal.com/api/v1/notifications"]]; 
//create the Method "POST" 
[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 
[urlRequest setValue:@"Basic O645372ZjItM2NiOC00ZjQ2LTk4Y2UtYjFlMjE5ODBiYzg2" forHTTPHeaderField:@"Authorization"]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
NSURLSessionUploadTask *dataTask = [session uploadTaskWithRequest: urlRequest 
                  fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
                   if(data == nil && error){ 
                    NSLog(@"uploadTaskWithRequest error: %@", error); 
                   } 
                   else{ 
                    id jsonResp = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
                    if([jsonResp isKindOfClass:[NSDictionary class]]){ 
                     NSDictionary *dictRes = [jsonResp copy]; 
                     NSLog(@"The dictRes is - %@",dictRes); 
                    } 
                    else{ 
                     NSArray *arrRes = [jsonResp copy]; 
                     NSLog(@"The dictRes is - %@",arrRes); 
                    } 
                   } 
                  }]; 
[dataTask resume]; 

我是从Create notification - Sends notifications to your users One Signal

查找的文档的NSURLSession(URLSession如果你使用Swift) - 如果你想在iOS上进行联网,这是一个很好的起点。