Aws-sdk-ios: How to using AWSSQS in swift 2

Created on 10 Dec 2015  路  4Comments  路  Source: aws-amplify/aws-sdk-ios

Do you have any example about using AWSSQS in swift ?
I don't find any code about it . I really want to use it but i don't know way to use.

question

Most helpful comment

I ran into this exact issue recently. Had to look at the actual API code (Obj-C!), http://docs.aws.amazon.com/AWSiOSSDK/latest/, and Java+REST versions of the SQS API. My use case was a simple message pushing to a specified queue.

Below is my solution - hope it helps the above users a bit. I've just started learning/using Swift, so the solution/style may not be the best. If anyone has a better way of doing this, please post it :)

// Don't forget to set up your credentials in AppDelegate.swift and
// the imports in your Obj-C bridging-header.

let queueName = "YOUR_QUEUE_NAME"
let sqs = AWSSQS.defaultSQS()

// Get the queue's URL
let getQueueUrlRequest = AWSSQSGetQueueUrlRequest()
getQueueUrlRequest.queueName = queueName
sqs.getQueueUrl(getQueueUrlRequest).continueWithBlock { (task) -> AnyObject! in
   print("Getting queue URL")
   if let error = task.error {
      print(error)
   }
   if let exception = task.exception {
      print(exception)
   }

   if task.result != nil {
      if let queueUrl = task.result!.queueUrl {
         // Got the queue's URL, try to send the message to the queue
         let sendMsgRequest = AWSSQSSendMessageRequest()
         sendMsgRequest.queueUrl = queueUrl
         sendMsgRequest.messageBody = "MY TEST MESSAGE!"

         // Add message attribute if needed
         let msgAttribute = AWSSQSMessageAttributeValue()
         msgAttribute.dataType = "String"
         msgAttribute.stringValue = "MY ATTRIBUTE VALUE"
         sendMsgRequest.messageAttributes = [:]
         sendMsgRequest.messageAttributes!["MY_ATTRIBUTE_NAME"] = msgAttribute

         // Send the message
         sqs.sendMessage(sendMsgRequest).continueWithBlock { (task) -> AnyObject! in
            if let error = task.error {
               print(error)
            }
            if let exception = task.exception {
               print(exception)
            }

            if task.result != nil {
               print("Success! Check the queue on AWS console!")
            }
            return nil
         }
      } else {
         // No URL found, do something?
      }
   }
   return nil
}

All 4 comments

We currently do not have a sample App for AWS SQS in Swift. I will post it as a feature request to my team and will update this thread once we have a sample out. Thanks!

-Rohan

May be you have any example about using AWSSQS in Obj-C?
I don't find any simple and clear examples how to use your AWSSQS on both language.

Can you provide simple example how to send any message into sqs queue?

Could you describe your use case so that we can better understand your needs? Amazon SQS has limited use case on the mobile device, and it will help us prioritize SQS related tasks. Thanks.

I ran into this exact issue recently. Had to look at the actual API code (Obj-C!), http://docs.aws.amazon.com/AWSiOSSDK/latest/, and Java+REST versions of the SQS API. My use case was a simple message pushing to a specified queue.

Below is my solution - hope it helps the above users a bit. I've just started learning/using Swift, so the solution/style may not be the best. If anyone has a better way of doing this, please post it :)

// Don't forget to set up your credentials in AppDelegate.swift and
// the imports in your Obj-C bridging-header.

let queueName = "YOUR_QUEUE_NAME"
let sqs = AWSSQS.defaultSQS()

// Get the queue's URL
let getQueueUrlRequest = AWSSQSGetQueueUrlRequest()
getQueueUrlRequest.queueName = queueName
sqs.getQueueUrl(getQueueUrlRequest).continueWithBlock { (task) -> AnyObject! in
   print("Getting queue URL")
   if let error = task.error {
      print(error)
   }
   if let exception = task.exception {
      print(exception)
   }

   if task.result != nil {
      if let queueUrl = task.result!.queueUrl {
         // Got the queue's URL, try to send the message to the queue
         let sendMsgRequest = AWSSQSSendMessageRequest()
         sendMsgRequest.queueUrl = queueUrl
         sendMsgRequest.messageBody = "MY TEST MESSAGE!"

         // Add message attribute if needed
         let msgAttribute = AWSSQSMessageAttributeValue()
         msgAttribute.dataType = "String"
         msgAttribute.stringValue = "MY ATTRIBUTE VALUE"
         sendMsgRequest.messageAttributes = [:]
         sendMsgRequest.messageAttributes!["MY_ATTRIBUTE_NAME"] = msgAttribute

         // Send the message
         sqs.sendMessage(sendMsgRequest).continueWithBlock { (task) -> AnyObject! in
            if let error = task.error {
               print(error)
            }
            if let exception = task.exception {
               print(exception)
            }

            if task.result != nil {
               print("Success! Check the queue on AWS console!")
            }
            return nil
         }
      } else {
         // No URL found, do something?
      }
   }
   return nil
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fs-bonlook picture fs-bonlook  路  3Comments

anbarasu0504 picture anbarasu0504  路  3Comments

cornr picture cornr  路  4Comments

dougboberg picture dougboberg  路  5Comments

pawlowskialex picture pawlowskialex  路  4Comments