Reputation: 27214
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];
Upvotes: 2
Views: 7571
Reputation: 19310
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]);
}
Upvotes: 0
Reputation: 10775
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.
Upvotes: 7
Reputation: 75286
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
Upvotes: 1