<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emanuele</title>
    <description>The latest articles on DEV Community by Emanuele (@ema987).</description>
    <link>https://dev.to/ema987</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F906631%2F8ab20275-69b5-40e8-a3fa-36aae6f715f1.png</url>
      <title>DEV Community: Emanuele</title>
      <link>https://dev.to/ema987</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ema987"/>
    <language>en</language>
    <item>
      <title>Kotlin Multiplatform and Swift - Overcoming Interoperability Challenges for Multiplatform Development</title>
      <dc:creator>Emanuele</dc:creator>
      <pubDate>Sun, 16 Jul 2023 10:21:19 +0000</pubDate>
      <link>https://dev.to/ema987/kotlin-multiplatform-and-swift-overcoming-interoperability-challenges-for-multiplatform-development-5ae2</link>
      <guid>https://dev.to/ema987/kotlin-multiplatform-and-swift-overcoming-interoperability-challenges-for-multiplatform-development-5ae2</guid>
      <description>&lt;p&gt;&lt;a href="https://kotlinlang.org/lp/multiplatform/"&gt;Kotlin Multiplatform Mobile (KMM)&lt;/a&gt; is a popular framework for developing multi-platform mobile applications with shared code between Android and iOS. &lt;br&gt;
However, one limitation of KMM is that it doesn't allow direct use of Swift libraries: this is because Kotlin doesn't have direct interoperability with the Swift language; instead, it relies on interoperability with Objective-C.&lt;/p&gt;

&lt;p&gt;As a result, KMM doesn't have built-in support for Swift-only libraries that don't have an Objective-C API. This means that developers can't use Swift libraries directly in a KMM project without first creating a bridging library that provides a Kotlin interface to the Swift code.&lt;/p&gt;

&lt;p&gt;Creating a bridging library requires writing a separate library for each Swift library that needs to be used in the project: this can be time-consuming and requires knowledge of both Kotlin and Swift.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using a Swift library with Dependency Injection and Koin
&lt;/h2&gt;

&lt;p&gt;Imagine the classical situation where you want to use a third party service and they provide you a native Android SDK and a native iOS SDK (written in Swift).&lt;br&gt;
If you are going to develop two native apps, you face no issues, each platform implements and uses their respective SDK.&lt;br&gt;
If you are going to develop a KMM app, you end up having some issues due to the fact the iOS SDK is written in Swift.&lt;/p&gt;

&lt;p&gt;Let's see how we can use &lt;a href="https://insert-koin.io/"&gt;Koin&lt;/a&gt; to achieve Dependency Injection (DI) and incorporate a third-party iOS Swift SDK in a KMM project.&lt;br&gt;
Koin supports KMM development, making it the ideal choice for KMM projects.&lt;/p&gt;
&lt;h1&gt;
  
  
  Shared code
&lt;/h1&gt;

&lt;p&gt;If you are going to use a library which already supports KMM, you are going to declare it as a dependency in the &lt;code&gt;commonMain&lt;/code&gt; source set of the &lt;code&gt;shared&lt;/code&gt; module &lt;code&gt;build.gradle.kts&lt;/code&gt; file.&lt;br&gt;
This is not our case, we have a library for Android and a separate one for iOS.&lt;br&gt;&lt;br&gt;
For the sake of example, let's imagine we have &lt;code&gt;com.weather.sdk:1.0.0&lt;/code&gt; for Android and &lt;code&gt;WeatherSDK&lt;/code&gt; for iOS: they provide weather information given a city.&lt;/p&gt;

&lt;p&gt;So what do we do?&lt;/p&gt;
&lt;h2&gt;
  
  
  Create a wrapper
&lt;/h2&gt;

&lt;p&gt;To avoid scattering the use of the weather SDK throughout the project and allow for future flexibility to swap it out with another SDK, we create a wrapper interface for it. This approach provides a layer of abstraction that enables us to decouple the SDK implementation from the rest of the project, making it easier to maintain and modify in the future.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;WeatherDataSource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cityName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;WeatherInfo&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not take care of the function signature, it's just an example. Keep the focus on the fact we are creating an interface to decouple from the Weather SDK.&lt;/p&gt;

&lt;p&gt;Now, we need to implement this interface both for Android and for iOS. The implementantions will use the respective Weather SDK to fetch data.&lt;br&gt;
Let's start from Android first, which is easier.&lt;/p&gt;
&lt;h2&gt;
  
  
  Android setup
&lt;/h2&gt;

&lt;p&gt;First, set the Android library as dependency in the &lt;code&gt;androidMain&lt;/code&gt; source set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;androidMain&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;getting&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.weather.sdk:1.0.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes us able to use &lt;code&gt;com.weather.sdk&lt;/code&gt; into the &lt;code&gt;androidMain&lt;/code&gt; source set.&lt;/p&gt;

&lt;p&gt;Next step is to create the implementation for &lt;code&gt;WeatherDataSource&lt;/code&gt;, so we create &lt;code&gt;AndroidWeatherDataSource&lt;/code&gt;, where we use the &lt;code&gt;weatherSdk&lt;/code&gt; class of Weather SDK to fetch weather information.&lt;br&gt;&lt;br&gt;
&lt;em&gt;This is the link between our code and the third party SDK.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AndroidWeatherDataSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;weatherSdk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WeatherSDK&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;WeatherDataSource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cityName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;WeatherInfo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//use weatherSdk to fetch weather information!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, still in the &lt;code&gt;androidMain&lt;/code&gt; source set, we create this function to init Koin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * [context] is the Android app context
 * [appModules] is a list of modules defined in the app layer (e.g. viewModels)
 */&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;initKoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appModules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;startKoin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;androidLogger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;androidContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appModules&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sharedModules&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;weatherModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Koin will be setup using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;appModules&lt;/code&gt;: modules created in the app layer, e.g. for viewModels&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sharedModules&lt;/code&gt;: modules created in the &lt;code&gt;commonMain&lt;/code&gt; source set to be shared between platforms&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;weatherModule&lt;/code&gt;: module where we declare dependencies for &lt;code&gt;com.weather.sdk&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will concentrate on &lt;code&gt;weatherModule&lt;/code&gt;, let's see its code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;weatherModule&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;singleOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;AndroidWeatherDataSource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nc"&gt;WeatherDataSource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We declare &lt;code&gt;AndroidWeatherDataSource&lt;/code&gt; as single instance for whenever we require &lt;code&gt;WeatherDataSource&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's move to iOS!&lt;/p&gt;

&lt;h2&gt;
  
  
  iOS setup
&lt;/h2&gt;

&lt;p&gt;iOS setup it's a little bit more cumbersome than the Android one, but let's go step by step to see what's needed.&lt;/p&gt;

&lt;p&gt;Since the iOS library is written in Swift and doesn't have Objective-C bindings, we can't declare it in the &lt;code&gt;iOSMain&lt;/code&gt; source set, we can't &lt;a href="https://kotlinlang.org/docs/native-c-interop.html"&gt;&lt;code&gt;cinterop&lt;/code&gt;&lt;/a&gt; it neither.&lt;br&gt;
The only way to use it is from the iOS project that AndroidStudio/KMMPlugin created for us.&lt;/p&gt;

&lt;p&gt;First thing, as we did for Android, it's to create the implementation for &lt;code&gt;WeatherDataSource&lt;/code&gt;, so we create &lt;code&gt;IOSWeatherDataSource&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;WeatherSDK&lt;/span&gt;
&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="n"&gt;shared&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;IOSWeatherDataSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherDataSource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;weatherSDK&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherSDK&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;weatherSDK&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;WeatherSDK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;weatherSDK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;weatherSDK&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getWeather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cityName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;WeatherInfo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//use weatherSdk to fetch weather information!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's go back to our &lt;code&gt;shared&lt;/code&gt; module into &lt;code&gt;iOSMain&lt;/code&gt; source set: here, as well as for Android, we need to create a function to init Koin.&lt;br&gt;
Right now, Koin API can't be used in the iOS project, so we need both to create a function to init Koin and to expose all Koin dependencies to the iOS project.&lt;br&gt;&lt;br&gt;
To do so we create a class &lt;code&gt;DependenciesProviderHelper&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DependenciesProviderHelper&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KoinComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * [weatherDataSource] is the iOS implementation to fetch weather information
     */&lt;/span&gt;
    &lt;span class="nf"&gt;initKoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;weatherDataSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WeatherDataSource&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;iosDependenciesModule&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;single&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;weatherDataSource&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nc"&gt;WeatherDataSource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;startKoin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iosDependenciesModule&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sharedModules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//usecases&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;getWeatherInfoUseCase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;GetWeatherInfoUseCase&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since it's currently not possible to use Koin APIs from iOS, the &lt;code&gt;initKoin()&lt;/code&gt; function can't have a list of Koin &lt;code&gt;Module&lt;/code&gt; like the Android one. However, we can still provide the real implementations of our dependencies and create a Koin module in the shared code. Then, we can use &lt;code&gt;startKoin()&lt;/code&gt; with both the iOS dependencies and the shared modules.&lt;/p&gt;

&lt;p&gt;Imagine then that this &lt;em&gt;DataSource&lt;/em&gt; will be used in a &lt;em&gt;Repository&lt;/em&gt; that will be used in a &lt;em&gt;UseCase&lt;/em&gt;, here in &lt;code&gt;DependenciesProviderHelper&lt;/code&gt; we also expose &lt;code&gt;getWeatherInfoUseCase&lt;/code&gt; to be able to use it from the iOS project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Koin!
&lt;/h2&gt;

&lt;p&gt;Both the projects are now setup correctly, Dependency Injection is in place, real implementations have been created, we just miss one last thing: to finally start Koin!&lt;/p&gt;

&lt;p&gt;On Android, you start Koin in your application class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApplication&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;initKoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="nd"&gt;@MyApplication&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appModules&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, you do the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@main&lt;/span&gt;
&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;MyApplication&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;iosWeatherDataSource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="c1"&gt;//create iOSWeatherDataSource&lt;/span&gt;
        &lt;span class="c1"&gt;//DependenciesProviderHelper should be a singleton&lt;/span&gt;
        &lt;span class="kt"&gt;DependenciesProviderHelper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initKoin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nv"&gt;weatherDataSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;iosWeatherDataSource&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Instances injected by Koin
&lt;/h1&gt;

&lt;p&gt;What instances does Koin provide in our code and how does it provide them, based on the code above?&lt;br&gt;
&lt;code&gt;AndroidWeatherDataSource&lt;/code&gt; is defined as a single instance for &lt;code&gt;WeatherDataSource&lt;/code&gt; on Android, which means that Koin will handle its instantiation and provide it whenever it is needed in our code. Because it is defined as single, the instance will remain the same throughout the lifecycle of our app.&lt;br&gt;
On iOS, the approach is similar, but Koin will not instantiate &lt;code&gt;IOSWeatherDataSource&lt;/code&gt; on its own. Instead, we will need to instantiate it ourselves and provide the instance to &lt;code&gt;startKoin()&lt;/code&gt; before Koin can manage it and provide it whenever it is needed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Considerations
&lt;/h1&gt;

&lt;p&gt;We have come to the end of how to use a Swift third-party library in a Kotlin Multiplatform project. While it's still necessary to write two different native implementations (one for iOS and one for Android), these implementations are restricted in the &lt;em&gt;DataSource&lt;/em&gt; level, while &lt;em&gt;UseCases&lt;/em&gt; and &lt;em&gt;Repositories&lt;/em&gt; can still be shared and written into the &lt;code&gt;commonMain&lt;/code&gt; source set.&lt;/p&gt;

&lt;p&gt;This means that the &lt;em&gt;UseCases&lt;/em&gt; can use shared &lt;em&gt;Repositories&lt;/em&gt;, and tests can be written to cover the &lt;em&gt;UseCases&lt;/em&gt; and &lt;em&gt;Repositories&lt;/em&gt; code just once. If there's a bug in the &lt;em&gt;DataSources&lt;/em&gt;, it will be spotted by these tests.&lt;/p&gt;

&lt;p&gt;The shared code also provides the advantage of being able to implement functionality once, such as a cache in the &lt;em&gt;Repository&lt;/em&gt; layer, instead of writing it twice for both platforms.&lt;/p&gt;

&lt;p&gt;While there are limitations and challenges to using Swift third-party libraries in a Kotlin Multiplatform project, the benefits can make it a worthwhile investment for developers looking to build multiplatform apps.&lt;/p&gt;

</description>
      <category>android</category>
      <category>ios</category>
      <category>kotlinmultiplatform</category>
      <category>koin</category>
    </item>
  </channel>
</rss>
