iOS PubSub Sample
June 1, 2026 ยท View on GitHub
This sample demonstrates how to establish an MQTT connection with the AWS IoT Core message broker and execute MQTT operations using the MQTT 5 Client in iOS Application.
The sample would perform the following actions:
- Initializes the Device SDK library
- Sets up the MQTT Client
- Opens the MQTT connection
- Subscribes to test topics
- Publishes to test topics
- Closes the MQTT connection
Before Running the Sample
Setup an AWS Account:
If you don't have an AWS account, complete these steps to create one. This will provide you with an account specific endpoint.
Understand IoT:
The What is AWS IoT developer guide will help you understand IoT.
Prepare Your IoT Thing certificates
While you create an IoT thing, please download the certificate files into iOSPubSubSample\MqttClient folder. Save the certificate files as .\iOSPubSubSample\MqttClient\cert.pem, and the private key file as .\iOSPubSubSample\MqttClient\privatekey.pem.
How to Run the Sample (with XCode)
- Launch the
iOSPubSubSample\MqttClient.xcodeprojectwith XCode - Setup your endpoint in
iOSPubSubSample\MqttClient\ContentView.swift: Replace "" in the source with your IoT endpoint value.
let TEST_HOST = "<your-endpoint>"
- Run the project. You should see the the following screen

- Test the operation:
- Click "Setup Client and Start" to start a connection session.
- Click "Publish A Message" to send a publish message.
- Click "Stop Connection" to stop a connection session.
Troubleshooting
Enable logging in samples
To enable logging in the samples, you must add the following line after initializing IotDeviceSdk. The logger level has the following options: trace, debug, info, warn, error, fatal, or none.
// The IoT Device SDK must be initialized before it is used.
IotDeviceSdk.initialize();
// This will turn on SDK and underlying CRT logging to assist in troubleshooting.
try Logger.initialize(target: .standardOutput, level: .debug)
I'm getting error code 5134: AWS_ERROR_MQTT_UNEXPECTED_HANGUP
This error is most likely due to your AWS IoT Core thing's policy. The policy must provide privileges for this sample to connect. The following is a sample policy that can be used on your AWS IoT Core thing that allows this sample to run as intended.
For the purposes of this sample, make sure your policy allows a client ID of test-* to connect or use the --client_id <client ID here> argument to use a client ID that your policy supports.
(see sample policy)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect"
],
"Resource": [
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
]
}
]
}
Replace the following with the data from your AWS account:
<region>: The AWS Region where you created the AWS IoT Core thing you wish to use with this sample. For example,us-east-1. For more information, see AWS IoT Core endpoints.<account>: Your AWS account ID. For more information, see View AWS account identifiers
Note: In a real application, you might want to avoid the use of wildcards in your policy or use them selectively. Follow best practices when using the SDK to work with AWS on production applications.
Other Resources
Check out our resources to learn more: