Firebase in SwiftUI
Add Firebase SDK
Learn how to add Firebase SDK to a SwiftUI project using SPM
In this short tutorial, you will learn how to integrate the Firebase SDK with a SwiftUI project using SPM (Swift Package Manager)
Create Firebase Project
- Go to the Firebase Console, click on Add Project, and type in the project name. If you want to change the Project ID, click on the generated ID, and rename it.
- Continue.
- Based on your preference, you can enable/disable the Google Analytics.
- If you enabled the Google Analytics for Firebase project, you will prompted with “Configure Google Analytics” to choose or create a Google Account.
- Click Create project to finish the creation process.
Register iOS app
Now that the project is created.. let’s register the iOS app to the Firebase project.
- Click the iOS+ icon to launch the setup workflow. Enter your app’s bundle ID in the Apple bundle ID field. The App nickname is optional, but I find it helpful, especially if you have more than one app in the Firebase project. click Register app
- Download GoogleService-Info.plist and move it into the root of your Xcode project.
Next, add the Firebase SDK using SPM, using the following steps in Xcode.
- In Xcode, go to File → Add Package Dependencies…
- Then enter
https://github.com/firebase/firebase-ios-sdk
in the search box, and click Add Package. - Select libraries you need to include in your project, and click Add Package.
If you’re using Xcode 15, you might face a bug that you won’t be able to select packages (checkbox should be on the left of each package), to fix that.. after you add package, navigate to Target → Build Phases and expand Link Binary With Libraries, here you can add or remove libraries.
I will also leave a link to another solution by BlueDefendr that might help, check the last answer in the this forum link on Raddit.
- Add the
-ObjC
flag if you are using Firebase Analytics in your project.. select project Target → Build Settings, search for Other Linker Flags and add-ObjC
.
Next, you will be prompted with the initialization code to configure Firebase in your SwiftUI project.
Before continuing with this part, you need to understand the app delegate swizzling.. This article explains the two approaches and when to use them. I highly recommend reading it!
If you already know/understand it or not interested, scroll down to TL;TR, for the final initialization code.
According to Firebase documentation, in SwiftUI applications we must disable swizzling.
In a short summary…
So if you are using any of the four products mentioned above, you need to use the @UIApplicationDelegateAdaptor
property wrapper to connect your app to an instance of AppDelegate
. And initialize Firebase inside App Delegate’s application(_:didFinishLaunchingWithOptions:)
.
Otherwise.. you can initialize Firebase in your app’s main entry point.
Problem
Pre-iOS 16, App Delegate’s application(_:didFinishLaunchingWithOptions:)
used to run before the app’s main entry point, so initializing Firebase in the AppDelegate
was sufficient to meet the requirements, while still initializing any environmentObjects
needed at the launch of the app.
But now post iOS 16, if you initialize any environmentObjects
that depends on Firebase initialization, app will crash with the following error:
Solution
The solution is to move Firebase initialization FirebaseApp.configure()
into the app’s main entry point, while keeping the @UIApplicationDelegateAdaptor
property wrapper to connect your app to an instance of the AppDelegate
.
TL;TR:
Add the FirebaseAppDelegateProxyEnabled
flag in info.plist
and set it to NO
.
The following in the final initialization code for your app’s main entry point:
Run the app 📲 .. it is now successfully connected to Firebase.
With everything set up, Continue to console to explore and start adding Firebase products to your app.
Resources
“ Everyone has something to learn. Everyone has something to teach.” ~ Paul Hudson