Skip to content

Instantly share code, notes, and snippets.

Created Jul 16, 2019
What would you like to do?
func connectToAWSIoT(clientId: String!) {
func mqttEventCallback(_ status: AWSIoTMQTTStatus ) {
switch status {
case .connecting: print("Connecting to AWS IoT")
case .connected:
print("Connected to AWS IoT")
// Register subscriptions here
// Publish a boot message if required
case .connectionError: print("AWS IoT connection error")
case .connectionRefused: print("AWS IoT connection refused")
case .protocolError: print("AWS IoT protocol error")
case .disconnected: print("AWS IoT disconnected")
case .unknown: print("AWS IoT unknown state")
default: print("Error - unknown MQTT state")
// Ensure connection gets performed background thread (so as not to block the UI) .background).async {
do {
print("Attempting to connect to IoT device gateway with ID = \(clientId)")
let dataManager = AWSIoTDataManager(forKey: "kDataManager")
dataManager.connectUsingWebSocket(withClientId: clientId,
cleanSession: true,
statusCallback: mqttEventCallback)
} catch {
print("Error, failed to connect to device gateway => \(error!)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment