9. Integrating KMP with an iOS App

Daniyar Nurgaliyev
2 min readJun 21, 2024

--

Follow these steps to set up your iOS project using Xcode and integrate it with the shared module.

Ensure Shared Module Configuration

During the creation of your KMP setup, the Android Studio wizard generates configuration code in your …/shared/build.gradle.kts file. This configuration is essential for the iOS project to work with the shared module. The code looks like this:

kotlin {
...
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "shared"
isStatic = true
}
}
...
}

This code block does the following:

  • iosX64(), iosArm64(), iosSimulatorArm64() specifies the target platforms for iOS.
  • binaries.framework configures the creation of a framework binary for each target.
  • baseName sets the base name of the framework. Ensure this matches the module name created during the KMP setup.
  • isStatic indicates the framework should be static.

If this code is not present, add it and sync your project to ensure the iOS app can work with the shared module.

This leads to the creation of different folders, such as appleMain, iosArm64Main, etc.

If you don’t have that code, add it and sync to make the iOS app work with the shared module. Ensure that the name in baseName matches the module name created during the KMP setup. For our case, no additional steps are needed; everything works out of the box.

Use Shared Code in Xcode

Open ContentView.swift in Xcode. Import the shared module at the top of the file. Use the Greeting class from the shared module to display a greeting message.

import shared

struct ContentView: View {
...
var body: some View {
...
Text(Greeting().greet())
...
}
}

This ensures that your iOS project can access and utilize the shared code from the KMP setup.

After completing the setup, your Xcode project should look like this:

Summary

By following these steps, you can effectively integrate and utilize shared code between Android and iOS in your KMP setup, leveraging the strengths of both platforms in your mobile development process.

Unlisted

--

--