Sample code for 30+ languages & platforms
Objective-C

SQS Receive Message

See more Amazon SQS Examples

Retrieves one or more messages from an Amazon SQS queue, with a maximum limit of 10 messages, from the specified queue.

See SQS ReceiveMessage or detailed information.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <NSString.h>
#import <CkoXml.h>

BOOL success = NO;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoRest *rest = [[CkoRest alloc] init];

// Connect to the Amazon AWS REST server.
// such as https://sqs.us-west-2.amazonaws.com/
BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"sqs.us-west-2.amazonaws.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];

// Provide AWS credentials for the REST call.
CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
authAws.AccessKey = @"AWS_ACCESS_KEY";
authAws.SecretKey = @"AWS_SECRET_KEY";
// the region should match our URL above..
authAws.Region = @"us-west-2";
authAws.ServiceName = @"sqs";

[rest SetAuthAws: authAws];

[rest AddQueryParam: @"Action" value: @"ReceiveMessage"];
[rest AddQueryParam: @"MaxNumberOfMessages" value: @"5"];
[rest AddQueryParam: @"VisibilityTimeout" value: @"15"];
[rest AddQueryParam: @"AttributeName" value: @"All"];
[rest AddQueryParam: @"MessageAttributeName" value: @"All"];

// Use the actual path part of your SQS queue URL here:
NSString *responseXml = [rest FullRequestNoBody: @"GET" uriPath: @"/123456789123/chilkatTest"];
if (rest.LastMethodSuccess != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

// A successful response will have a status code equal to 200.
if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"response status code = ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@%@",@"response status text = ",rest.ResponseStatusText);
    NSLog(@"%@%@",@"response header: ",rest.ResponseHeader);
    NSLog(@"%@%@",@"response body: ",responseXml);
    return;
}

// Examine the successful XML response.
CkoXml *xml = [[CkoXml alloc] init];
[xml LoadXml: responseXml];
NSLog(@"%@",[xml GetXml]);
NSLog(@"%@",@"----");

// A successful response looks like this:
// <?xml version="1.0" encoding="utf-8" ?>
// <ReceiveMessageResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/">
//     <ReceiveMessageResult>
//         <Message>
//             <Body>this is a test</Body>
//             <MD5OfBody>54b0c58c7ce9f2a8b551351102ee0938</MD5OfBody>
//             <ReceiptHandle>AQEBMyLof...UbKBTvHtEg==</ReceiptHandle>
//             <Attribute>
//                 <Name>SenderId</Name>
//                 <Value>957491831129</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>ApproximateFirstReceiveTimestamp</Name>
//                 <Value>1475013283557</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>ApproximateReceiveCount</Name>
//                 <Value>4</Value>
//             </Attribute>
//             <Attribute>
//                 <Name>SentTimestamp</Name>
//                 <Value>1475013283557</Value>
//             </Attribute>
//             <MessageId>52882c65-5e21-4450-9024-93a3b01e61d3</MessageId>
//             <MD5OfMessageAttributes>2ff68603f4239272bd03f543254ed040</MD5OfMessageAttributes>
//         </Message>
//     </ReceiveMessageResult>
//     <ResponseMetadata>
//         <RequestId>05ba5c92-80ee-5f87-be76-0cf67b7475d6</RequestId>
//     </ResponseMetadata>
// </ReceiveMessageResponse>

// Get some values from the XML:
NSLog(@"%@%@",@"Body: ",[xml ChilkatPath: @"ReceiveMessageResult|Message|Body|*"]);
NSLog(@"%@%@",@"MessageId: ",[xml ChilkatPath: @"ReceiveMessageResult|Message|MessageId|*"]);

NSString *unused = [xml ChilkatPath: @"ReceiveMessageResult|Message[0]|Attribute|$"];

CkoXml *rec = [xml FindNextRecord: @"Name" contentPattern: @"SenderId"];
NSLog(@"%@%@",@"SenderId: ",[rec GetChildContent: @"Value"]);

rec = [xml FindNextRecord: @"Name" contentPattern: @"ApproximateFirstReceiveTimestamp"];
NSLog(@"%@%@",@"ApproximateFirstReceiveTimestamp: ",[rec GetChildContent: @"Value"]);

rec = [xml FindNextRecord: @"Name" contentPattern: @"ApproximateReceiveCount"];
NSLog(@"%@%@",@"ApproximateReceiveCount: ",[rec GetChildContent: @"Value"]);

rec = [xml FindNextRecord: @"Name" contentPattern: @"SentTimestamp"];
NSLog(@"%@%@",@"SentTimestamp: ",[rec GetChildContent: @"Value"]);

[xml GetRoot2];