Hi everyone :wave: I’m trying to figure out how to use Swift Package Manager (SPM) to import the Goo...
a
Hi everyone 👋 I’m trying to figure out how to use Swift Package Manager (SPM) to import the Google Sign-In SDK and make it available in Kotlin Multiplatform (KMP) common code via
expect/actual
. For example, I want my common code to have something like:
Copy code
expect class GoogleAuthProvider constructor(context: Any) {
    suspend fun signIn(): GoogleAccount?
    suspend fun signOut()
}
If I were using CocoaPods, I could do something like this in `iosMain/GoogleAuthProvider.ios.kt`:
Copy code
import cocoapods.GoogleSignIn.GIDSignIn
import com.peliplat.mobile.data.model.auth.socialAuth.GoogleAccount

GIDSignIn.sharedInstance.signInWithPresentingViewController(rootViewController)
That works because CocoaPods dependencies are automatically available to Kotlin via the
cocoapods.*
import. But in the case of SPM, I’m not sure: • How can I import the SPM dependency (Google Sign-In) so that I can call its APIs in
iosMain
Kotlin code? • Do I need to create a Swift wrapper for SPM dependencies and then expose them to Kotlin? • What is the recommended way to implement the
actual
class in KMP when using SPM instead of CocoaPods? I’m new to KMP, so any guidance or examples would be super helpful 🙏 Cc: @Orlando
🙌 1
plus one 1
❤️ 1
😇 1
a
Thanks a lot @Pablichjenkov, I will get back to you after following this.
👍 1
d
there is also https://github.com/frankois944/spm4Kmp from @François
p
Looks good, although generally the pattern consists of injecting a Factory or Provider rather than the instance itself. Because you may want to have multiple instances in the Kotlin side.
a
Thanks for pointing it out @Pablichjenkov, You're right, right now I am injecting singletons. Do you have any example of injecting a Factory or a provider in mind?
p
In the article I posted, check
SwiftLibDependencyFactory
class
Above class is what people call dependency bridge in kmp. You can have as many function factories as you want, in this example there is only one but you can have many.
The article uses
SwiftLibDependencyFactory
as a Singleton in swift side(the use of
shared
). But you can also create one single instance manually in the ApplicationDelegate and pass a swift injection framework root container, in case you are using dependency injection in Swift. Then having the root DI container from swift side, you can use it to resolve all the dependencies inside
SwiftLibDependencyFactory
This way bridging the two DI frameworks from each side, let's say Swinject and Koin.
If not using a DI framework in swift, then just do like in the article, manually create the class instance.