I have an Android/iOS project which has a Koin mod...
# koin
l
I have an Android/iOS project which has a Koin module in 
shared
 : CommonModule.kt:
Copy code
val commonModule = module {
    single<HttpClient> { TmdbApi.createHttpClient() }
    single<TmdbApi> { TmdbApi(client = get()) }
}

fun initKoin() = startKoin {
    modules(CommonModule.module)
}
In the Android app I can inject dependencies very easily:
Copy code
val tmdbApi: TmdbApi by inject()
In the iOS AppDelegate I call 
CommonModuleKt.doInitKoin()
How can I inject the dependencies in the iOS 
ContentView
?
m
I have this file: https://github.com/Oztechan/CCC/blob/develop/ios/CCC/DI/Koin.swift with the extensions there I am able to reach the dependencies with a pleasant syntax, ie like this:
Copy code
@StateObject var observable: CalculatorObservable = koin.get()
l
I created the extensions for iOS in a Kotlin file in the
shared
module inside `iosMain`: https://github.com/lukaszkalnik/TmdbApp4/blob/master/shared/src/iosMain/kotlin/com/kalnik/tmdbapp4/di/Koin.kt
Then in the
iosApp
module I created a
Koin.swift
file where I get the
Koin
instance from the shared module: https://github.com/lukaszkalnik/TmdbApp4/blob/master/iosApp/iosApp/Koin.swift
And on this
Koin
instance I can call the extension methods which I defined in
shared/iosMain
to get particular dependencies: https://github.com/lukaszkalnik/TmdbApp4/blob/99572639d32864b0a74baaa98bd31e1680259a63/iosApp/iosApp/TV%20Show%20Summary/TVShowsViewModel.swift#L7
s
I had “fixed” my issue in a pretty weird inline way just to make it work yesterday, but both of these two solutions are much nicer! Thank you both!
👍 3
l
I think I copied my solution from one of the other KMM samples, but don't remember which one
m
No worries 🙂 I wanted to keep the the way of getting dependency same for every variable ie:
Copy code
@StateObject var observable: CalculatorObservable = koin.get()
another file
Copy code
@StateObject var observable: SettingsObservable = koin.get()
so by this way I just use
koin.get()
everywhere