https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Adam Brown

06/03/2022, 4:29 AM
so I created a new KMP project, got some stuff up and working across Android & Desktop, and I want to add iOS now, which the project wizard didn't create anything for. Is there a good tutorial or anything for adding iOS to a KMP (not KMM) project after the fact?
r

ribesg

06/03/2022, 8:29 AM
It’s kinda simple nowadays, basically 1) Use a Mac 2) add target
ios()
3) Add folder for sourceSet
iosMain
There’s a little bit more to do if you’re on a M1 Mac but that’s really the gist of it
m

Matti MK

06/03/2022, 8:45 AM
Going on a slight tangent here, but could you elaborate on the extra steps required for M1? Thanks 👍
r

ribesg

06/03/2022, 8:49 AM
Yes! With a M1 Mac you will want to run an Arm64 simulator when testing and not a X64 simulator. For this you need to add another iOS target,
iosSimulatorArm64()
ios()
is a shortcut for a bunch of stuff that
iosSimulatorArm64()
does not do, so you have to do it yourself (it’s not much)
ios()
creates both
iosArm64()
and
iosX64()
targets, as well as sourceSets
iosMain
,
iosArm64Main
,
iosX64Main
for source files and
iosTest
,
iosArm64Test
and
iosX64Test
for tests, then links them together (
iosArm64Main.dependsOn(iosMain)
, etc.).
iosSimulatorArm64()
does not do the linking to the “iOS-common” `iosMain`/`iosTest` sourceSets, so you have to do that manually.
🙇 1
m

Matti MK

06/03/2022, 8:50 AM
Great info, thanks for sharing 👍 I’ve been developing apps on KMM for about 9mo now, but I’ve got an Intel so didn’t know these differences with the
sourceSets
and
targets
r

ribesg

06/03/2022, 8:53 AM
I don’t have a M1 Mac yet so this is still theory for me. I also don’t know if you can still run a X64 simulator on M1 using the bridging stuff which I forgot the name of, or not at all. What I can tell you is that you can already add the arm64 simulator target to a KMP project and compile it on a X64 Mac, that I tested and it works.
1
l

Landry Norris

06/03/2022, 3:36 PM
I don’t think that the iosArm64Simulator target is strictly required for M1. I don’t use it currently on my M1, and it looks like it falls back to x86_64. It’s possible I set that up using Rosetta 2 in the past, but I don’t have any issues currently.
a

Adam Brown

06/03/2022, 9:54 PM
So, adding
ios()
to the common module and resyncing does indeed break the other modules, then adding their source sets allows me to fill in that code. But the next step I think is that I need a seperate iOS module, much like how Desktop and Android have their own modules that actually include and run the common code. I have very little ios experience, was woundering if there were any good tutorials on dropping a new ios project in there and getting it to reference the common iOS module
l

Landry Norris

06/03/2022, 10:36 PM
If you look at the compose experimental samples, they show how to create the whole app in Kotlin without a separate xcode project.
a

Adam Brown

06/03/2022, 11:55 PM
Oh really? I want to have a swift UI though
the sort of frustrating thing, is if you just follow the happy path, File -> New Project, create a multiplatform project with iOS support, on a Mac, then try to build it, it fails in a variety of ways.
and all of the examples i've found online, are using a fairly different project structure than what the project generator creates, so its hard to know exactly how to make it work by just looking at them
l

Landry Norris

06/04/2022, 12:11 AM
That's fair. I would love direct Swift support in Kotlin, but until then, you need swift for SwiftUI
4 Views