Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
588 stars 737 forks source link

Using the Azure IoT Hub SDK for C in an iOS Swift app #2646

Open tom-botley opened 2 months ago

tom-botley commented 2 months ago

Hello, I am developing a library for an iOS app which is built in Swift. It just needs to achieve two things, connecting to the IoT Hub with a connection string and receiving cloud to device (C2D) messages.

Having read through the various documentation for the Azure IoT Hub SDK I am a little lost. Some of the documentation is still making reference and providing examples of using the now deprecated Cocoapods. I assume this is for legacy purposes and this would be a bad approach for new development. Instead I have been looking at the Azure IoT Hub SDK for C and wrapping that in Swift.

I have hit blockers with two approaches I have tried for this. The first is to create static libraries (.a files) by cross compiling the SDK for the iOS platform with CMake. I have setup the toolchain to target iOS but the resultant static libraries causing build errors in my test app such as the one below which suggests either the various CMakeLists.txt files or my toolchain are not setup to build for iOS correctly. Is there an official/recommended way to cross compile for iOS? Do I need to modify the SDKs CMakeLists.txt files or have I likely set up my toolchain file incorrectly?

Building for 'iOS', but linking in object file (Path/To/My/Project/AzureIoT/lib/libiothub_client.a[arm64]5) built for 'macOS'

Here is an example of one of the many toolchain files I have tried running:

# Toolchain file for building iOS static libraries

# Set the target system name and platform
set(CMAKE_SYSTEM_NAME iOS)
set(CMAKE_OSX_SYSROOT iphoneos)

# Minimum deployment target for iOS
set(CMAKE_OSX_DEPLOYMENT_TARGET "15.0")

# Architectures to build (arm64 for iOS devices)
set(CMAKE_OSX_ARCHITECTURES "arm64")

# Specify the C and C++ compilers
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")

# Configure output paths
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

# Enable Bitcode (required for iOS App Store submissions)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fembed-bitcode")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fembed-bitcode")

# Disable shared libraries (optional if only static libraries are needed)
set(BUILD_SHARED_LIBS OFF)

find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)

The other approach which seems to be the one used by the azure-sdk-for-c-swift repo sample is to have the C files brought over without building them into static libs and exposing them with headers. These C files however don't seem to be the ones found in the Azure IoT SDK for C repo so I am a little confused as to where they have come from and therefore if I can use the ones in the SDK repo without having to heavily modify them.

Any pointers on any of the issues and questions I have raised here would be much appreciated!