# How to use Kotlin Multiplatform ViewModel in SwiftUI and Jetpack Compose

DevFeed: [How to use Kotlin Multiplatform ViewModel in SwiftUI and Jetpack Compose](<https://devfeed.tech/articles/how-to-use-kotlin-multiplatform-viewmodel-in-swiftui-and-jetpack-compose-23905.md>)

Original publisher: [Read original article](<https://medium.com/icerock/how-to-use-kotlin-multiplatform-viewmodel-in-swiftui-and-jetpack-compose-8158e98c091d?source=rss----b74ae24564e---4>)

Author: Aleksey Mikhailov

Published: 2022-04-30T12:34:48Z

Content type: tutorial

Language: en

Sources: [IceRock Development - Medium](<https://devfeed.tech/sources/icerock-development-medium.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [Compose](<https://devfeed.tech/topics/compose.md>), [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [SwiftUI](<https://devfeed.tech/topics/swiftui.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android Studio](<https://devfeed.tech/topics/android-studio.md>)

Tags: [android-studio](<https://devfeed.tech/tags/android-studio.md>), [cocoapods](<https://devfeed.tech/tags/cocoapods.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [swift](<https://devfeed.tech/tags/swift.md>), [swiftui](<https://devfeed.tech/tags/swiftui.md>), [ui](<https://devfeed.tech/tags/ui.md>)

## AI overview

A tutorial showing how to use a shared Kotlin Multiplatform ViewModel with SwiftUI and Jetpack Compose. It uses MOKO MVVM 0.13.0 and demonstrates building an authorization screen for Android and iOS.

## Source excerpt

We at IceRock Development have been using the MVVM approach for many years, and the last 4 years our ViewModel are shared in the common code. We do it by using our library moko-mvvm. In the last year, we have been actively moving to using Jetpack Compose and SwiftUI to build UI in our projects. And it require MOKO MVVM improvements to make it more comfortable for developers on both platforms to work with this approach. On April 30, 2022, new version of MOKO MVVM -- 0.13.0 was released. This version has full support for Jetpack Compose and SwiftUI. Let's take an example of how you can use ViewModel from common code with these frameworks. The example will be simple -- an application with an authorization screen. Two input fields -- login and password, button Log in and a message about a successful login after a second of waiting (while waiting, we turn the progress bar). Create a project The first step is simple -- take Android Studio, install Kotlin Multiplatform Mobile IDE plugin, if not already installed. Create a project according to the template "Kotlin Multiplatform App" using CocoaPods integration (it's more convenient with them, plus we need it to connect an additional CocoaPod later). git commit Login screen on Android with Jetpack Compose The app template uses the standard Android View approach, so we need to enable Jetpack Compose before implementation of UI. Enable Compose support in androidApp/build.gradle.kts: val composeVersion = "1.1.1"android { // ... buildFeatures { compose=true } composeOptions { kotlinCompilerExtensionVersion = composeVersion } } And we add the dependencies we need, removing the old unnecessary ones (related to the usual approach with view): dependencies { implementation(project(":shared")) implementation("androidx.compose.foundation:foundation:$composeVersion") implementation("androidx.compose.runtime:runtime:$composeVersion") // UI implementation("androidx.compose.ui:ui:$composeVersion") implementation("androidx.compose.ui:ui-tooling