Compose Multiplatform Library as a Swift Package Manager in iOS

Chetansinh Rajput
Mobile Innovation Network
3 min readJun 11, 2024

When given the job of developing a component for both iOS and Android, we faced a crucial decision: should we use native code for each platform or embrace a cross-platform solution for a unified codebase? After weighing the pros and cons, we decided to dive into the world of cross-platform technologies. With the options of Flutter and React Native on the table, we found ourselves intrigued by the rising buzz around Kotlin Multiplatform (KMP) and Compose Multiplatform (CMP). Ultimately, we chose CMP for its promising potential.

Our journey with Compose Multiplatform was initially challenging, but the results were well worth the effort. We successfully created a robust component library using CMP. Now, the question arises: how do we integrate this library into an iOS project? There are several ways to achieve this, but today, we’ll focus on using Swift Package Manager (SPM). Let’s explore how we accomplished this integration and made our CMP library seamlessly work with iOS.

Generating a Swift Package from Your Compose Library

To integrate our Compose Multiplatform library into an iOS project, the first step is to generate a Swift package. This allows us to leverage Swift Package Manager (SPM) for seamless integration.

Step 1: Add the Plugin

To generate a Swift package, we used the `multiplatform-swiftpackage` plugin. Add the following line to the `build.gradle.kts` file in your common module:

id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"

Step 2: Configure the Plugin

Next, we set various configuration parameters needed by the plugin. The plugin’s README provides a full list of available options, but here are the essential ones we used:

multiplatformSwiftPackage {
packageName("PeopleInSpace")
swiftToolsVersion("5.3")
targetPlatforms {
iOS { v("13") }
}
}

Step 3: Generate the Swift Package

With these updates in place, run the following command to generate the Swift package:

./gradlew createSwiftPackage

This command generates the necessary files and writes them to the `common/swiftpackage` directory.

Now, you have a Swift package ready to be integrated into your iOS project using Swift Package Manager. This approach simplifies the process, allowing you to take full advantage of your Compose Multiplatform library in an iOS environment.

Using the Swift Package in Your iOS Project

Now that you’ve generated a Swift package from your Compose Multiplatform library, it’s time to integrate it into your iOS project. You have two options: adding the package locally or using a remote repository.

Option 1: Add the Package Locally

  1. Open Xcode and select your project.
  2. Navigate to `Project -> Package Dependencies`.
  3. Click `+ Add Package Dependency`.
  4. Select `Add Local` and choose the Swift package you generated.

Option 2: Add the Package from a Remote Repository

  1. Upload the Swift package to a Git repository (e.g., GitHub, GitLab).
  2. Open Xcode and select your project.
  3. Navigate to `Project -> Package Dependencies`.
  4. Click `+ Add Package Dependency`.
  5. Enter the URL of the Swift package repository and follow the prompts to add it to your project.

Conclusion

Bingo! Now you can seamlessly use your Compose Multiplatform library in your iOS project. This integration not only simplifies your workflow but also enhances the flexibility and reusability of your code across different platforms. Happy coding!

Connect with us 👇

Linkedin

GitHub

--

--