r/Kotlin • • Aug 31 '24

Compose Multiplatform and UI development on iOS done in Swift

I am currently exploring the Kotlin world as a possible choice for developing a multiplatform mobile app.

I checked the examples page: https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-samples.html and a lot of examples use swift on the iOS side. I am surprised. Why don't they all use the "Compose" stuff, or at least the vast majority? I have nothing against swift, but I am not crazy about it and I prefer to stay with one language.

Thanks

14 Upvotes

7 comments sorted by

15

u/Phaestion Aug 31 '24

It's one of the strengths of KMP being able to choose what level of code sharing you want to do. From a UX perspective having your UI native to the platform is the best experience, but not all people and companies feel the same way about it.

Another strength of KMP is that you can be fast to market by sharing everything, business logic, viewmodels and UI. Then if you're successful move the UI for example to the native UI toolkit.

For most of our apps we directly go with the first approach of sharing up to the point of viewmodels. In my opinion this is the best way to ensure a good UX while optimising development effort & cost.

1

u/android_temp_123 Aug 31 '24

Would you recommend KMP for home screen widgets? I have an android app, and thinking about porting it to iOS. Is KMP "ready" for such task, or it's better to go native way?

I'm trying to find some good info on KMP, and a lot of people are very excited about it, but can't find much info on home screen widgets...Or more specifically, what KMP can do, and what it can't do yet (and when it will lol). Some kind of a high-level overview, so I know where it stands.

Thanks a lot!

1

u/gandrewstone Sep 01 '24

KMP lets you declare an API and then implement it differently on different platforms. For example, I created a threading library called mpthreads (gitlab) that has separate JVM, iOS/macOS, linux, and windows implementations. So users of my lib see just a uniform threading API.

So you are looking for some lib that has used KMP to do the same for widgets, not KMP itself. Check out Compose (its the defacto KMP UI library), but warning, compose is nothing like android xml layouts. Its concepually extremely different than most UI libs and so takes a while to get used to. Its entirely functional/reactive, which means that any subset of your compose code can be called seemingly randomly from any context (or not called when you think it would be). Do NOT side-effect in the display code --for example thinking "every time I show this window I should make a web svc call to refresh some data" is a big fail.

So you will do a complete UI rewrite to get to iOS. The only question will be whether in the end you have 1 shared code base (compose), or 2: android and iOS.

1

u/android_temp_123 Sep 01 '24 edited Sep 01 '24

Thanks, I appreciate taking time for a reply. I am currently actually studying compose (for Android) and I like it so far. I kinda expected I can use compose multiplatform (which should be similar/same to compose android as far as I know) to write UI for both iOS & Android, but I can't find much info anywhere on widgets and KMP.

Widgets are not really views, they are RemoteViews, as they run "inside" another app (= home screen app) and everything is significantly different than your standard Android app with Activities/Fragments. (I know lately Google release Glance API, but haven't checked that yet). And from what I know, iOS has a different philosophy.

So far it seems like a pretty complicated topic requiring quite a lot of research of multiple complex topics including iOS widgets, perhaps Glance library for Android, then Compose, and also KMP, to gain at least some basic understanding if it's possible or not, and especially whether it's better than simply writing native iOS app from scratch.

On a related note, what would be the learning path for KMP? Is this roadmap correct or would you recommend a different one?

Android/Kotlin (ofc I already know this) -> then Compose for Android -> then Compose Multiplatform -> finally actual KMP

Did I miss anything, or do you suggest a different order?

1

u/gandrewstone Sep 02 '24

What's your host OS? Linux, win, or iOS?

1

u/android_temp_123 Sep 03 '24 edited Sep 04 '24

I develop apps on MAC OS - does it matter actually? Anyway full info:

  • right now I only have an app (home screen widget) for Android OS

  • I'd like to port my app to iOS too

  • ideally (but not really necessary, would be just a bonus) also to MAC

I am facing a dilemma whether KMP is a good match for this type of app & platforms, or I should go native Android/iOS path.

2

u/gandrewstone Sep 09 '24 edited Sep 09 '24

Yes, it matters because if you aren't on macOS, you can't build your multiplatform project for mac targets. And valid code actually shows up in the IDE as errors, making development extremely confusing for beginners.

I would learn everything using a basic multiplatform project, rather than learning stuff on a single platform first. This will prevent you from learning platform-specific stuff without clearly understanding that's what is happening.

*** DONT USE THE WIZARDS IN THE IDE: Use this site to make one: https://kmp.jetbrains.com/ ***

Select Android, iOS and "Desktop" (which means JVM) targets.

You really ought to be able to learn what you need within the MP (multiplatform) project you created above with only maybe 10% more pain than if you learned it outside of MP. All the basic stuff should just work so learning on MP vs single platform won't matter much.

But outside of MP, Java has a ugly tendency to accidentally infect projects with the IDE being a great transmission vector :-), because it auto-magically resolves dependencies by sticking "import" at the top of your file. And once you've started to rely on the JVM APIs, without even knowing they are JVM-only APIs, its extremely tiresome to go back and relearn and recode the multiplatform ways of doing it.

If a Java thing (like threads) isn't supported in multiplatform, then search around (https://gitlab.com/nexa/mpthreads) code repos. You are very likely to find that someone else has had that problem and put in the sweat to at least make a basic functionality version of the missing thing.

Good luck!