page_type | languages | products | description | |||
---|---|---|---|---|---|---|
sample |
|
|
A set of easy-to-understand, continuously-tested samples for connecting to Azure IoT Hub via Azure/azure-iot-sdk-c/CocoaPod. |
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
- This repository cloned or downloaded onto your development machine.
- An IoT hub in your Azure subscription. If you don't have an IoT hub, follow the steps in Create an IoT hub.
- A device registered in your IoT hub. If you don't have a device, follow the steps in Create and manage device identities to register a device and retrieve its device connection string.
- The latest version of XCode, running the latest version of the iOS SDK. This quickstart was tested with XCode 9.3 and iOS 11.3.
- The latest version of CocoaPods.
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:
- AzureIoTHubClient contains the Azure IoT Hub Client
- AzureIoTHubServiceClient contains the Azure IoT Hub Service Client
- AzureIoTUtility contains the Azure IoT C Shared Utility library
- AzureIoTuAmqp contains the Azure IoT AMQP library
- AzureIoTuMqtt contains the Azure IoT MQTT library
- azure-iot-sdk-c: contains the source code for Azure IoT C SDK, as well as the platform specific adaption layer for iOS