Is it possible to use SwiftUI in the ios source se...
# multiplatform
b
Is it possible to use SwiftUI in the ios source set of a multiplatform module?
d
AFAICS, the answer must be no because SwiftUI is a Swift-only library, but Kotlin can only access Objective-C interfaces.
d
No, you can't use SwiftUI in this way. The way to interface SwiftUI wth KMM is to keep the SwiftUI View layer in Swift, and then write (or 'borrow') some Swift code to consume a Kotlin
StateFlow
and bridge it to the iOS 'Combine' reactive framework. Use this to bind the SwiftUI delcaration to View-Models of your KMM
shared
framework.
This method can lead to the 'gold standard' Mobile experience; where each platform is using its true native UI, while the logic (including presentation logic) is shared.
j
another interesting option, in this context of approach @darkmoon_uk describes, is using https://github.com/rickclephas/KMM-ViewModel
b
Yeah that's what we're doing but our kmp code is in another repo from our native so looking for solutions to improve the dev ex. It's simple enough for android to add a module next to the kmm code but wondering how to locate the swift ui code close to the multiplatform code.
Trying to avoid publishing all our kmp modules for every change and replicate the android experience where you can point to source
d
In an iOS app, the multiplatform code is an ObjectiveC-compiled framework, so any change to the multiplatform code needs a new compilation. Ideally you want to make the multiplatform code stable first, by testing it with the Android app.
I suggest to work on the iOS side only when the multiplatform code is stable, so it doesn’t need recompilations. In a multiplatform project, the priority is to stabilize the shared-code first.
l
You can also keep some swift code in your KMM repo for the UI, then statically link your KMM framework to your swift framework either manually or via cocoapods.
b
But if I now try to link this swift framework to my app won't I end up reincluding kotlin dependencies for every swift module linked this way?
d
@bnvinay92 not sure what you mean. Can you explain your use case better?