Sitemap
Swift Productions

Introducing Swift Productions: weekly articles and tutorials to learn how to build an App using Swift, SwiftUI & Firebase.

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

3 min readDec 7, 2021

--

Press enter or click to view image in full size
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

Press enter or click to view image in full size
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

Press enter or click to view image in full size
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.

Press enter or click to view image in full size
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.

Press enter or click to view image in full size
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
Press enter or click to view image in full size
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:

Press enter or click to view image in full size
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.

Press enter or click to view image in full size
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!

Press enter or click to view image in full size
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.

--

--

Swift Productions
Swift Productions

Published in Swift Productions

Introducing Swift Productions: weekly articles and tutorials to learn how to build an App using Swift, SwiftUI & Firebase.

Sullivan De Carli
Sullivan De Carli

Written by Sullivan De Carli

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

Responses (1)