I am building a KMP app using CMP, i have a share...
# multiplatform
z
I am building a KMP app using CMP, i have a shared login screen which navigates to a main screen that has a bottom navigation bar, the contents of the screen can fragments/composables on android, but on iOS the contents should be swiftUI pre built screens from a swift UIkit. Is there a way to host the swift UI views in fragments or composables?
c
z
Yes, but this example only shows how i can create a viewController in the iOS app, i have not managed to understand how it could help me display a swift UI inside a composable in an Android app.
c
In this snippet the most important part for you is the call to the
createUIViewController
This is passed to your shared Compose code from the iOS side of your project. In this example it is as simple as returning a swiftUI Text wrapped in a
UIHostingController
but this
createUIViewController
is defined by you. You can add parameters based on which you can return different swiftUI components.
Copy code
ComposeUIViewController {
        Column(
            Modifier
                .fillMaxSize()
                .windowInsetsPadding(WindowInsets.systemBars),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text("How to use SwiftUI inside Compose Multiplatform")
            UIKitViewController(
                factory = createUIViewController,
                modifier = Modifier.size(300.dp).border(2.dp, Color.Blue),
            )
        }
    }
Take a look at the provided Sample app, and play around with it, to get a better understanding about how it works https://github.com/JetBrains/compose-multiplatform/tree/master/examples/interop/ios-swiftui-in-compose
There is also a brand new Lib from Touchlab that helps with this exact scenario: https://touchlab.co/compose-swift-bridge-launch
👀 1
z
This was the answer, this plugin is amazing!
👍 1
c
Nice! I’m glad it helped you!