How to add Firebase to a SwiftUI Project with SPM
Short tutorial on how to integrate the Firebase SDK with the new SwiftUI lifecycle using Swift Package Manager
Create a new project
Go to Xcode > Create a new project > SwiftUI interface & lifecycle > Name it PMStart
Add a project on Firebase
Head to Firebase.google.com > Go to console > Add a new project
Add the iOS App to the Firebase project
First, go to the main menu > + Add App > Select iOS platform > Add Firebase to your Apple app.
Copy the bundle ID from Xcode main target and paste it in the field, then click on register app.
Add the config file
Download the GoogleService-info.plist file and drag this file inside your project folder in Xcode, it is important to add this file to the project, otherwise the app will crash with Firebase inizialized.
Add Firebase with Swift Package Manager
In Xcode, from the navigation at the top, go to File > Add packages > enter the URL below > Select the packages ☑️> Add Package
https://github.com/firebase/firebase-ios-sdk.git
I recommend you to add at least Firebase Analytics, FirebaseFirestore and FirebaseAuth and FirebaseStorage. Once it is done, you can click on Next on the screen below:
Add initialization code
Since we have created a SwiftUI, the code is a bit different from what is presented in the screen below.
You need to go to PMStartApp file which is the starter file for your app and add the following lines of codes:
import Firebase
And this code over the body variable:
init() {FirebaseApp.configure()}
Run the App
You can now run your app, it is successfully connected to the Firebase backend!
Would you like to kick start your Firebase project? Consider buying my book with tutorials on how to create iOS apps in SwiftUI with the Firebase backend.
You can also subscribe to enjoy unlimited access to my articles and all of Medium through my referral link.