# Can't inject dependencies in iOS

**URL:** <https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466>\
**Category:** Kotlin Multiplatform by Tutorials\
**Tags:** ios, android\
**Created:** [July 15, 2023, 2:25pm UTC](https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466 "2023-07-15T14:25:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![novemio](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/novemio/32/105896_2.png) [@novemio](https://forums.kodeco.com/u/novemio)\
**Post date:** [July 15, 2023, 2:25pm UTC](https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466/1 "2023-07-15T14:25:29Z")

</div>

Hello, I am building a KMM project and am using Koin for DI. Everything works fine on Android  
I just want to get the repository inside the viewModelWrapper on iSO. There I use  
`let repository: Repository = Koin.instance.get()`  
in order to get the repository from Koin

In iOSMain, by following the tutorial I have `KoinIOS.kt`  
`  
import com.russhwolf.settings.NSUserDefaultsSettings  
import com.russhwolf.settings.Settings  
import kotlinx.cinterop.ObjCClass  
import kotlinx.cinterop.getOriginalKotlinClass  
import org.koin.core.Koin  
import org.koin.core.KoinApplication  
import org.koin.core.parameter.parametersOf  
import org.koin.core.qualifier.Qualifier  
import org.koin.dsl.module  
import platform.Foundation.NSUserDefaults

object KoinIOS {  
fun initialize(  
userDefaults: NSUserDefaults,  
): KoinApplication = initKoin(  
appModule = module {  
single {  
NSUserDefaultsSettings(userDefaults)  
}  
}  
) // swift doesn’t bridge Kotlin functions with default parameters. This functions is to compensate for that limitation  
}

fun Koin.get(objCClass: ObjCClass): Any {  
val kClazz = getOriginalKotlinClass(objCClass)!!  
return get(kClazz, null, null)  
}

fun Koin.get(objCClass: ObjCClass, qualifier: Qualifier?, parameter: Any): Any {  
val kClazz = getOriginalKotlinClass(objCClass)!!  
return get(kClazz, qualifier) { parametersOf(parameter) }  
}

actual val platformModule = module {}  
`

I `iOSApp` there is `Koin.swift` with the following:  
`  
import shared

final class Koin {  
private var core: Koin\_coreKoin?

static let instance = Koin()

static func start() {  
if instance.core == nil {  
let app = KoinIOS.shared.initialize(  
userDefaults: UserDefaults.standard  
)  
instance.core = app.koin  
}  
if instance.core == nil {  
fatalError(“Can’t initialize Koin.”)  
}  
}

private init() {  
}

func get\<T: AnyObject\>() → T {  
guard let core = core else {  
fatalError(“You should call `start()` before using (#function)”)  
}

```
guard let result = core.get(objCClass: T.self) as? T else {
  fatalError("Koin can't provide an instance of type: \(T.self)")
}

return result

```

}  
}  
`

Error I am getting at the moment is when I call `Koin.instance.get()` from `iOSMain` and then it tries to execute  
`guard let result = core.get(objCClass: T.self) as? T else {fatalError("Koin can't provide an instance of type: \(T.self)") }`

Error is  
`Thread 1: EXC_BAD_ACCESS (code=1, address=0x310)`

Here is the screenshot of the error I get on XCode

 ![Screenshot 2023-07-15 at 4.21.29 PM](https://files.chunter.kodeco.com/original/3X/1/d/1d77d00ede7a64aecc65f88b74a74cfc8a5eb249.jpeg)

---

<div class="post-metadata">

**Author:** ![cafonsomota](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cafonsomota/32/108553_2.png) [@cafonsomota](https://forums.kodeco.com/u/cafonsomota)\
**Post date:** [July 17, 2023, 3:54pm UTC](https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466/2 "2023-07-17T15:54:29Z")

</div>

Hello novemio,

Could you please tell us which version of Kotlin and Koin you’re currently using?

Thank you

---

<div class="post-metadata">

**Author:** ![novemio](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/novemio/32/105896_2.png) [@novemio](https://forums.kodeco.com/u/novemio)\
**Post date:** [July 21, 2023, 6:34pm UTC](https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466/3 "2023-07-21T18:34:20Z")

</div>

Hello! Koin version `3.1.5`, Kotlin version `1.8.21`

---

<div class="post-metadata">

**Author:** ![sdtaheri](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sdtaheri/32/111366_2.png) [@sdtaheri](https://forums.kodeco.com/u/sdtaheri)\
**Post date:** [August 17, 2023, 11:50am UTC](https://forums.kodeco.com/t/cant-inject-dependencies-in-ios/189466/4 "2023-08-17T11:50:38Z")

</div>

Hello @novemio

Sorry to get back to you this late. I just tried setting up a test KMM project with Koin 3.1.5 and Kotlin 1.8.21 on AS Giraffe. I tried following the steps in chapter 9 of the book and the app seemed to work.

Since we don’t have access to the project and setup you have, we can’t find your issue with certainty; however, here are some steps you can take to find the issue:

1. Make sure you’re defining your modules correctly. If you forget to define the modules, Koin will fail to find and inject them.
2. Make sure to call `Koin.start()` or whatever initializer you’ve defined when your iOS app starts before calling any of those `Koin.instance.get(...)`. The best place to do so is in the `init` block of your `App` instance in Swift.
3. When an app crashes in Xcode, it’s best to select the crash point in your own code. For instance, in the picture you attached, it’s best to select row 4 or 3 in the sidebar. The one that you selected doesn’t help that much.

I hope these tips would help.
