page_type: sample languages:
azure-iot-samples-iot provides a set of easy-to-understand, continuously-tested samples for connecting to Azure IoT Hub via Azure/azure-iot-sdk-c/CocoaPod
In this section, you simulate an iOS device running a Swift application to receive cloud-to-device messages from the IoT hub.
CocoaPods manages dependencies for iOS projects that use third-party libraries.
In a terminal window, navigate to the folder containing this repository on your development machine. Then, navigate to the sample project folder:
cd quickstart/sample-device
Make sure that XCode is closed, then run the following command to install the CocoaPods that are declared in the podfile file:
pod install
Along with installing the pods required for your project, the installation command also created an XCode workspace file that is already configured to use the pods for dependencies.
Retrieve the connection string for your device. You can copy this string from the Azure portal in the device details page, or retrieve it with the following CLI command:
az iot hub device-identity connection-string show --hub-name {YourIoTHubName} --device-id {YourDeviceID} --output table
Open the sample workspace in XCode.
open "MQTT Client Sample.xcworkspace"
Expand the MQTT Client Sample project and then folder of the same name.
Open ViewController.swift for editing in XCode.
Search for the connectionString variable and update the value with the device connection string that you copied in the first step.
Save your changes.
Run the project in the device emulator with the Build and run button or the key combo command + r.
You're now ready to receive cloud-to-device messages. Use the Azure portal to send a test cloud-to-device message to your simulated IoT device.
In the iOS App Sample app running on the simulated IoT device, select Start. The application starts sending device-to-cloud messages, but also starts listening for cloud-to-device messages.
In the Azure portal, navigate to your IoT hub.
Select Device management > Devices from the IoT Hub menu.
On the Devices page, select the device ID for your simulated IoT device.
Select Message to Device to open the cloud-to-device message interface.
Write a plaintext message in the Message body text box, then select Send message.
Watch the app running on your simulated IoT device. It checks for messages from IoT Hub and prints the text from the most recent one on the screen. Your output should look like the following example: