Skip to content

Bridge The Native API

How It Works

When syncing the project, the plugin creates a folder at src/swift/[cinteropName] or src/swift/[targetName] if cinteropName is not defined, for example src/swift/iosArm64.

The content of this folder is your bridge between Swift and Kotlin, everything inside is copied to the build directory.

StartYourBridgeHere.swift

A template file named StartYourBridgeHere.swift is added when the bridge is empty; it contains some example and this is your starting point to create your bridge.

This behavior can be disable by adding inside your gradle.prodperties spmforkmp.disableStartupFile=true

Example

Gradle

The following configuration is a simple bridge between Kotlin and the Swift Apple Native SDK.

The plug-in uses the cinterop feature of KMP to export the compatible code to your Apple target code.

build.gradle.kts
kotlin {
    listOf(
        iosArm64(),
        iosSimulatorArm64()
        // and more Apple targets...
    ).forEach { target ->
        target.swiftPackageConfig(cinteropName = "[cinteropName]") {
        }
    }
}
Legacy (< 1.1.0)
build.gradle.kts
swiftPackageConfig {
    create("[cinteropName]") {
    }
}

Bridge

Make your Swift code compatible with Kotlin.

Your Swift code needs to be marked as @objc/@objcMembers and the visibility set as public or it won't be exported and available from your Kotlin code.

A playground to help you to import Swift code into your Kotlin code.

src/swift/[cinteropname]/mySwiftFile.swift
import UIKit

@objcMembers public class MySwiftBridge: NSObject {
    public func exportedMethod() -> String {
        return "value"
    }
    public func exportedView() -> NSObject {
        return UIView()
    }
}
iosMain/kotlin/com/example/myKotlinFile.kt
import [cinteropname].MySwiftBridge

val contentFromSwift = MySwiftBridge().exportedMethod()

val aView = MySwiftBridge().exportedView() as UIView

Handle Resources (since v1.6.0)

To handle resources in your bridge, like images, fonts, etc.

  • Add a folder named Resources and put your resources inside like bridgeString.txt.
  • Access from Swift with Bundle.module.url(forResource: "bridgeString", withExtension: "txt").