AWS Developer Tools Blog
Announcing the General Availability of the AWS IoT Device SDK for Swift
We are excited to announce the General Availability (GA) of the AWS IoT Device SDK for Swift. This release gives Swift developers a production-ready SDK with stable APIs and integrated service clients to connect applications to AWS IoT Core.
What’s New
The GA release now provides easy-to-configure service clients for three essential AWS IoT Core services:
- AWS IoT Device Shadow: Synchronize and share data between devices, apps, and other cloud services.
- AWS IoT Jobs: Manage remote operations that can run on devices connected to AWS IoT Core.
- Device provisioning: Automatically create the certificates and policies needed for secure communication, eliminating manual certificate management.
The SDK supports macOS, iOS, tvOS, and Linux, with X.509 certificate-based authentication and TLS 1.3 encryption on iOS and tvOS. For a detailed overview of platform and security capabilities, see Introducing the AWS IoT Device SDK for Swift (Developer Preview).
Getting Started
The SDK provides service client samples (Device Shadow, Jobs, and Device provisioning) on the GitHub website. The following walkthrough demonstrates how to set up a Shadow client and retrieve a shadow state.
Prerequisites
Before you start with the service client, set up the required AWS IoT resources: For more information, see What is AWS IoT? and Create AWS IoT resources.
After you complete these steps, you will have three items required for client configuration:
- Your IoT endpoint
- Your X.509 certificate file
- Your associated private key file
Use AWS IoT Shadow client
Step 1: Add the SDK dependency
The IoT Shadow client is a product within the aws-iot-device-sdk-swift package. Add the package as a dependency and reference the Shadow client in your target’s Package.swift file:
let package = Package(
name: "MyApp",
dependencies: [
.package(
url: "https://github.com/aws/aws-iot-device-sdk-swift.git",
from: "1.0.0"
),
],
targets: [
.executableTarget(
name: "MyApp",
dependencies: [
.product(name: "IotShadowClient", package: "aws-iot-device-sdk-swift"),
]
)
]
)
Step 2: Create an MQTT 5 client
Before you create a Shadow client, create an MQTT 5 client. This example uses the endpoint and certificate files from the Prerequisites section.
// Create an Mqtt5ClientBuilder configured using a certificate and private key
let clientBuilder = try Mqtt5ClientBuilder.mtlsFromPath(
endpoint: self.endpoint,
certPath: self.cert,
keyPath: self.key)
// Create the MQTT5 client using the Mqtt5ClientBuilder and start a connection session
let client = try builder.build()
client.start()
For more information about the certificates, see X.509 client certificates.
Step 3: Create the Shadow client
After you start the MQTT 5 client, configure the client options and create the Shadow client. These options to cap the client’s subscription usage and reserve capacity for other parts of your IoT application.
Configure the following options based on your application’s subscription needs:
maxRequestResponseSubscription: Maximum number of concurrent subscriptions that request-response client uses. Each request usually uses 1-2 subscriptions until completion.maxStreamingSubscription: Maximum number of concurrent streaming operation subscriptions that the client will allow. Set based on the number of streaming operations you plan to use simultaneously.operationTimeout: Request timeout in seconds.
The following example creates a Shadow client with values that work well for applications that use a single shadow with limited streaming operations. Adjust these values based on your application’s needs:
// Set up options for the MqttRequestResponseClient
let options = MqttRequestResponseClientOptions(
maxRequestResponseSubscription: 3,
maxStreamingSubscription: 2,
operationTimeout: 5)
// Create a Shadow client using the MQTT5 client and the options created above
let shadowClient = try IotShadowClient(mqttClient: client, options: options)
Step 4: Perform Shadow Operations
With the Shadow client ready, you can perform operations such as retrieving, updating, or deleting a shadow state. The following example retrieves a named shadow state:
let request: GetShadowRequest = GetShadowRequest(thingName: inputThingName)
do {
let response = try await shadowClient.getShadow(request: request)
} catch {
// Log errors
}
For more information, see the Shadow sample on the GitHub website. Additional service client samples are also available on GitHub:
Known Limitations
The SDK has the following known limitations:
- TLS 1.3 on macOS: While TLS 1.3 is not currently supported on macOS, we are actively developing support and will add it in a future release. This limitation does not affect iOS, tvOS, or Linux platforms.
- HTTP Proxy Support: HTTP proxy support is available on macOS and Linux only; it is not currently supported on iOS and tvOS.
For updates on these limitations, see GitHub Discussions.
Conclusion
In this post, we showed you how to get started with the AWS IoT Device SDK for Swift from adding the SDK dependency to performing shadow operations. The SDK also includes clients for Jobs and Device Provisioning. Use the Jobs client to manage remote device operations or the Device Provisioning client to automate certificate creation at scale.
For more information, see Getting started with AWS IoT Core tutorials and connect your first device to AWS IoT Core.
Let us know how you’re using the SDK in the comments. You can also:
- Share ideas and ask questions in GitHub Discussions.
- Report issues through GitHub Issues.