Skip to content

Instantly share code, notes, and snippets.

@mgwedd

mgwedd/awsiot-5.js Secret

Created Jul 16, 2019
Embed
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
print(error.localizedDescription)
return [:]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.