Skip to content

About

SwiftUI Firebase KMP Sample

Resources

Stars

7 stars

Watchers

5 watching

Forks

Repository files navigation

official project

SwiftUI Firebase KMP Sample

An iOS and macOS app with a SwiftUI frontend and a Kotlin Multiplatform library that talks to Firebase. The Kotlin library gets the Firebase Apple SDK through the Kotlin Gradle Plugin's SwiftPM import (swiftPMDependencies {}), and both the library and the SwiftUI previews can run against a mock instead of Firebase.

SwiftPM import is the recommended way to add Firebase to a Kotlin Multiplatform project.

Note

The CocoaPods integration is deprecated. Firebase stops publishing new versions to CocoaPods in October 2026, and Firebase 12 is the last major version there (Firebase announcement). The earlier CocoaPods version of this sample is kept, unmaintained, on the cocoapods branch.

The app supports email/password sign-up, sign-in and sign-out, stores user profiles in Firestore, and logs events to Firebase Analytics.

Project structure

kotlin-library is the KMP module with the Firebase logic.

  • build.gradle.kts lists the Firebase SwiftPM products that Kotlin code uses directly. You don't need to list transitive modules such as FirebaseCore or FirebaseInstallations; the plugin imports them automatically.
  • FirebaseKMP is the API the Swift app calls. FirebaseKMPImpl implements it with Firebase, and MockFirebaseKMPImpl implements MockFirebaseKMP for tests and previews.
  • The library targets iOS and macOS, so most code lives in appleMain. appleMain/.../samples has one sample per Firebase product: App Check, A/B Testing, Crashlytics, Realtime Database, Cloud Functions, Installations, Remote Config, Storage, and Firebase AI Logic. App Distribution, In-App Messaging and Performance exist only for iOS, so their products are limited to iOS in build.gradle.kts and their samples are in iosMain.
  • FirebaseAIBridge is a local Swift package that wraps the Swift-only FirebaseAILogic SDK in @objc classes. SwiftPM import uses cinterop, which only sees Objective-C compatible declarations, so a wrapper like this is how Kotlin code can call a Swift-only library. The module adds it with localSwiftPackage(...).

appleApp is the SwiftUI app. One Xcode target builds it for iPhone and iPad (iOS 18) and for Mac (macOS 15). The bundle ID ends in .iosApp because it has to match the app registered in GoogleService-Info.plist.

  • FirebaseService.swift is an @Observable model that turns the callback-based Kotlin API into async functions.
  • Views/ has the login and registration screen (AuthView) and the profile screen (ProfileView).
  • KotlinMultiplatformLinkedPackage is generated by the Kotlin Gradle Plugin, and the Xcode project references it as a local package to link the SwiftPM dependencies into the app. Commit it, but don't edit it by hand.

Setup

  1. Install Xcode 26.2 or later (Firebase 13 requires it) and IntelliJ IDEA or Android Studio. You need a Mac.
  2. Create a Firebase project, register an iOS app, and put its GoogleService-Info.plist in appleApp/appleApp. Enable Email/Password sign-in and set up Firestore security rules.
  3. Open appleApp/appleApp.xcodeproj in Xcode, pick an iOS or macOS destination, and run the app. The Compile Kotlin Framework build phase builds the Kotlin library with ./gradlew :kotlin-library:embedAndSignAppleFrameworkForXcode.
  4. To sign in on a Mac, select your development team and add the Keychain Sharing capability to the appleApp target. Firebase Auth on macOS keeps the signed-in user in the keychain.

Changing SwiftPM dependencies

After you edit swiftPMDependencies {} in kotlin-library/build.gradle.kts, the next Xcode build regenerates KotlinMultiplatformLinkedPackage and fails with "Synthetic linkage package files changed during the build" and "Please go to File -> Package -> Resolve Package Versions in Xcode". Resolve the packages or build again, then commit the regenerated files along with .swiftpm-locks/default/swiftImport/Package.resolved, which pins the package versions used on the Kotlin side.

About

SwiftUI Firebase KMP Sample

Resources

Stars

7 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages