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

Sullivan De Carli
Swift Productions
3 min readDec 7, 2021

--

SwiftUI with Xcode and Firebase logo
Firebase + SwiftUI + Xcode and SPM

Create a new project

Go to Xcode > Create a new project > SwiftUI interface & lifecycle > Name it PMStart

Screenshot of a new project in Xcode
Xcode > New project

Add a project on Firebase

Head to Firebase.google.com > Go to console > Add a new project

Screenshot from a new project in Firebase
Create a new Firebase 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.

Firebase, add a new project

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.

Firebase, tutorial on how to add the config file.

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
Xcode, adding Firebase SDK with the Swift Package Manager

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:

Firebase, adding the Firebase SDK with Swift Package Manager

Add initialization code

Since we have created a SwiftUI, the code is a bit different from what is presented in the screen below.

Firebase, adding the initialization code

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!

Screenshot from Xcode with simulator running
Simulator running with Firebase installed

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.

--

--

Sullivan De Carli
Swift Productions

Consultant in iOS Dev at Deloitte. I write about Design & App development. Let’s have a chat at hello@sullivandecarli.com