iOS¶
Use the ios/app product type in a module to build an iOS application.
Using IntelliJ IDEA?
Make sure to install the Kotlin Multiplatform plugin to get proper support for Kotlin Native and iOS integration features.
Module layout¶
Here is an overview of the module layout for an iOS application:
my-ios-app/
├─ src/
│ ├─ KotlinCode.kt # (1)!
│ ├─ EntryPoint.swift # (2)!
│ ╰─ Info.plist
├─ module.yaml
╰─ module.xcodeproj # (3)!
- Kotlin code is optional. Everything could come from dependencies.
- The entrypoint of iOS apps must be a
@mainstruct in a Swift file. Read more - An Xcode project is required but it can be automatically generated by Amper the first time. Read more
Xcode Project¶
Currently, an Xcode project is required to build an iOS application in Amper.
It has to be named module.xcodeproj and located in the ios/app module root directory.
Normally, when the Amper project is created via amper init or via the IDE's Wizard, the appropriate Xcode project is
already there. This is currently the recommended way of creating projects that have an iOS app module.
However, if the Amper project is created from scratch, the default buildable Xcode project will be created automatically after the first project build. This project can later be customized and checked into a VCS.
If you want to migrate an existing Xcode project so it has Amper support, you must manually ensure that:
- it is named
module.xcodeprojand is located in the root of theios/appmodule - it has a single iOS application target
- the target has
Debug&Releasebuild configurations, each containingAMPER_WRAPPER_PATH = <relative path to amper wrapper script>. The path is relative to the Amper module root. - the target has a script build phase called
Build Kotlin with Amperwith the code:# !AMPER KMP INTEGRATION STEP! # This script is managed by Amper, do not edit manually! "${AMPER_WRAPPER_PATH}" tool xcode-integration - The Framework Search Paths (
FRAMEWORK_SEARCH_PATHS) option contains the$(TARGET_BUILD_DIR)/AmperFrameworksvalue
Changes to the Xcode project that do not break these requirements are allowed.
So the iOS app module layout looks like this:
├─ src/
│ ├─ KotlinCode.kt # optional, if all the code is in the libraries
│ ├─ EntryPoint.swift
│ ├─ Info.plist
├─ module.yaml # ios/app
╰─ module.xcodeproj # xcode project
Tip
The Xcode project can be built normally from the Xcode IDE, if needed.
Application entry point¶
For iOS applications, the entry point is expected to be a @main struct in any Swift file in the src folder.
├─ src/
│ ├─ main.swift
│ ╰─ ...
├─ module.yaml
╰─ module.xcodeproj
src/main.swift:
...
@main
struct iosApp: App {
...
}
This is not customizable at the moment.
Swift support¶
Info
Swift sources are only fully supported in the src directory of the ios/app module.
While swift sources are, strictly speaking, managed by the Xcode project and, as such,
can reside in arbitrary locations, it's not recommended to have them anywhere outside the src directory - the tooling
might not work correctly.
To use Kotlin code from Swift, one must import the KotlinModules framework.
This framework is built from:
- the code inside the
ios/appmodule itself - the modules that
ios/appmodule depends on (e.g.- ../shared) - all the external dependencies, transitively
Note
All declarations from the source Kotlin code are accessible to Swift, but external dependencies are not.