2

I am trying to achieve the following in 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/

Is there some sort of library I can use that achieves this? Obviously I have all of the values ready to go ahead and make my request, but I'm not exactly sure how to do it in Objective-C.

I am using TouchJSON, however I'm not quite sure how to construct the correct JSON payload above and POST this to the server (also I'd prefer this to be an asynchronous request rather than a synchronous).

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];
Brian
  • 14,610
  • 7
  • 35
  • 43
gotnull
  • 26,454
  • 22
  • 137
  • 203

3 Answers3

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

or send asynchronous request with

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

have a look at NSMutableURLRequest Class Reference to see how to set what.

Kai Huppmann
  • 10,705
  • 6
  • 47
  • 78
  • 5
    Exactly. Also using `sendSynchronousRequest:returningResponse:` is rarely a good idea. You should use the async method calls `NSURLConnection` provides if you want to have a usable application in the end. – toto May 13 '11 at 12:43
  • @toto: +1 edited my answer. But always depends on request times and what your app will be like, i.e. if it's able to work without the response. – Kai Huppmann May 13 '11 at 12:45
  • 1
    @toto: I would say that using `sendSynchronousRequest:returningResponse:` in a controlled background thread *(never the main thread)* actually leads to code that is easier to test, maintain and extend. – PeyloW May 13 '11 at 14:59
  • @peylow Agreed. E.g. using it in a `NSOperation` can have it's advantages, but it gets misused a _lot_, so I phrase it as " rarely", to make that clear. – toto May 15 '11 at 16:44
  • Ended up doing it in C# in the end. But this did work as expected in Objective-C. Thanks. – gotnull May 16 '11 at 04:55
1

You can use NSURLConnection for this; here's a sample:

How do I make HTTP post request for getting JSON object in response for iPhone application?

There may be a way to actually run a curl script proper, using NSTask: http://borkware.com/quickies/one?topic=nstask

Community
  • 1
  • 1
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
0

I searched a lot to convert this cUrl request to send Urban Airship push notifications, I finally did it below is the source code which worked for me:
Required: ASIHTTPRequest //Required to execute Http request

+(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]);

}
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70