Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2019 15:41
What would you like to do?
func registerSubscriptions() {
func messageReceived(payload: Data) {
let payloadDictionary = jsonDataToDict(jsonData: payload)
print("Message received: \(payloadDictionary)")
// Handle message event here...
let topicArray = ["topicOne", "topicTwo", "topicThree"]
let dataManager = AWSIoTDataManager(forKey: "kDataManager")
for topic in topicArray {
print("Registering subscription to => \(topic)")
dataManager.subscribe(toTopic: topic,
qoS: .messageDeliveryAttemptedAtLeastOnce, // Set according to use case
messageCallback: messageReceived)
func jsonDataToDict(jsonData: Data?) -> Dictionary <String, Any> {
// Converts data to dictionary or nil if error
do {
let jsonDict = try JSONSerialization.jsonObject(with: jsonData!, options: [])
let convertedDict = jsonDict as! [String: Any]
return convertedDict
} catch {
// Couldn't get JSON
return [:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment