iOS target
- Kotlin course
- Expert
- Lesson 28/47
- 5 min
Learn the iOS target with KMP.
iOS target
In KMP the iOS target compiles Kotlin code into a framework for iOS apps. Swift or Objective-C can call this framework.
The target is added in Gradle with iosArm64() (device), iosX64() (Intel simulator) and iosSimulatorArm64() (Apple Silicon simulator) blocks. The output is a framework that is embedded into an Xcode project. The iosTarget function in this lesson shows the concept with a simple model.
The framework output
The framework produced by the iOS target is embedded into the Xcode project. On the Swift side it is imported with import.
In Gradle the binaries { framework { baseName = "Shared" } } block sets the framework name. Swift code calls import Shared to use Kotlin classes. Type mapping is automatic: Kotlin String maps to Swift String, List to Array. This is the bridge between iOS and shared code.
Practice Write iosTarget
Define fun iosTarget(): String. It should return "iOS". In main, write println(iosTarget()).
Starter code
// add the iosTarget function
fun main() {
// print the iOS target
}Sample solution
fun iosTarget(): String = "iOS"
fun main() {
println(iosTarget())
}Expected output
iOS
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the iosArm64() block produce?
The iOS output is a framework.- .jar file
- framework Correct answer
- .exe file
- .so file
Explanation: iosArm64() produces a framework for iOS.
2 Complete the iosArm64 call
____() {
binaries { framework() }
}
iosArm64() {
binaries { framework() }
}
Explanation: iosArm64() adds the iPhone target and produces a framework.
3 Match the iOS parts
| Term | Meaning |
|---|---|
iosArm64() |
iPhone |
iosX64() |
Intel simulator |
framework |
iOS library |
Swift |
calls framework |