Hey i have created kmp project with native uis but...
# multiplatform
d
Hey i have created kmp project with native uis but i want share one paywall screen between ios and android which is coming from library dependencies , i have the common business logic in shared module, can i share paywall screen composable in ios app too ?
k
Yes you can. First configure your common main to use compose. Then you can do as following. commonMain:
Copy code
@Composable
fun YourComposableScreen() {}
you'll be able to use it directly in your android native side, but on ios you'll have to wrap it in ComposeUiViewContoller like this, iosMain:
Copy code
create a file named YourComposableScreenShared.kt

add this function in it

fun ComposableScreen() = ComposeUiViewController {
     YourComposableScreen()
}
then you'll be able to use it in swift ui code like this.
Copy code
import shared

struct PayWallScreen: View {

    
    var body: some View {
        VStack {
            YourComposableScreenShared()
        }
    
}

struct YourComposableScreenShared: UIViewControllerRepresentable {
    
    func makeUIViewController(context: Context) -> UIViewController {
        let content = YourComposableScreenSharedKt.ComposableScreen()
        
        return content
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    }
}
d
do i need to write it in shared module ? shared module doesn't have compose dependencies
k
Yep, you'll have to write it in shared module, and for that you'll have to first configure your shared module with compose multiplatform
d
or can i import composeapp binaries in ios using build gradle
k
I haven't tried that, but I don't think that'd be a simpler solution
d
but composeapp will have that screen already, which one is less expensive?
k
The one I suggested above, it's more scalable and easier... Generating binaries will make you run in to unforeseen issues.
d
so i would need to readjust all compose multiplatform dependencies to shared module from composeapp (this dependencies will be included in composeapp i presume), or should i simple include cmp dependencies directly in shared module and let them overlap with composeapp module
k
You just need to add compose plugin
Copy code
id("org.jetbrains.compose")
then in common source set, you can add compose dependencies from this plugins as you need, for very basic you'll need these.
Copy code
implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material)
                implementation(compose.ui)
Are you using kotlin > 2.0.0?
d
exactly 2.0.0
k
Are you already using compose compiler plugin?
d
yes
k
If you're using it in android gradle right now, you'll have to move that to shared..
d
okay was it there before kotlin 2.0.0 ?
k
Nope.. it is for kotlin 2.0.0 and above.