AWS Developer Tools Blog

Introducing the AWS IoT Device SDK for Swift (Developer Preview)

Today, AWS launches the developer preview of the AWS IoT Device SDK for Swift. The IoT Device SDK for Swift empowers Swift developers to create IoT applications for Linux and Apple macOS, iOS, and tvOS platforms using the MQTT 5 protocol.

The SDK supports Swift 5.10+ and is designed to help developers easily integrate with AWS IoT services.

Key features of the AWS IoT Device SDK for Swift

  • Swift integration: Leverages the modern Swift programming language to develop IoT applications.
  • Platform compatibility: Designed to support macOS, iOS, tvOS, and Linux.
  • TLS 1.3 support: Built-in TLS 1.3 support on Apple iOS and tvOS platforms, ensuring IoT applications use the latest industry-wide security practices for protecting data in-transit.MQTT version 5 (MQTT 5) support: MQTT 5 provides many benefits over earlier protocol versions, including error handling and reporting, enhanced scalability with shared subscriptions, and customizable user properties. See the MQTT 5 User Guide to learn more.
  • Async APIs: The SDK uses asynchronous APIs, which makes it easier for you to develop responsive applications.

Getting started

The AWS IoT Device SDK for Swift samples demonstrate simple and idiomatic ways to develop your applications using the MQTT 5 client to access AWS IoT services.

The following code sample demonstrates how to set up an MQTT client that uses mTLS for authentication.The Mqtt5ClientBuilder simplifies setting up the MQTT 5 client and connections using Swift.

let cert_filepath = "<certificate file path>"
let private_key_filepath = "<private key file path>"

let clientBuilder = try Mqtt5ClientBuilder.mtlsFromPath(
                                           certPath: cert_filepath, 
                                           keyPath: private_key_filepath, 
                                           endpoint: your_iot_endpoint)

let mqttClient = try clientBuilder.build()

// Open the connection after the client is built
try client.start()

After the client connects, you can subscribe to topics and publish messages on them. The publish and subscribe operations use an asynchronous API, making the code easy to reason about while benefiting from concurrent execution.

// Setup a publish packet
let publishPacket: PublishPacket = PublishPacket(qos: .atLeastOnce,
                                                 topic: "test/topic", 
                                                 payload: "HELLO WORLD!".data(using: .utf8))
// Async-await on publish operation
let publishResult: PublishResult = try await client.publish(
                                                 publishPacket: publishPacket)
if let puback = publishResult.puback {
    print(
        "Publish operation finished with reasonCode: \(puback.reasonCode)"
    )
}

The IoT Device SDK for Swift includes a full list of samples to demonstrate different connection methods:

iOS and tvOS support

With support for iOS and tvOS, the SDK integrates seamlessly with Apple’s keychain for secure credential storage and retrieval. To help you get started, we provide a dedicated iOS PubSub sample that demonstrates MQTT 5 subscription and message publishing operations.

After you launch the sample app (as shown below), you can interact with it to set up an MQTT client. You can use the client to establish a connection to the AWS IoT Core message broker and publish messages to AWS IoT Core. The sample app will also display any received messages on the test topic.

Screenshot of the iOS Sample

Summary

This blog post provides a quick introduction to the new IoT device SDK for Swift. We will continue adding more features and enhancements in the coming months. Stay tuned to theSDK Github page for updates. Feel free to leave your feedback in the comments.

Next steps

Ready to get started? To learn more, see the following resources: