[MUSIC PLAYING]
PETER FRIESE: Hey, everyone.
This is Peter from the Firebase team.
In this video, I'm going to show you
how to get started with Firebase on iOS.
And along the way, I will provide some interesting side
notes and behind-the-scenes details
that you might find useful.
Speaking of which, did you know that you
can use Firebase on Apple's other platforms as well?
That's right.
You can use most of Firebase's APIs
in your iOS, iPadOS, macOS, tvOS, and watchOS applications.
And we are continuously improving API coverage.
So whenever I say your iOS app in this video,
I mean all of those platforms as well.
All right, let's get into it.
So here is a little sample app I created
for demonstration purposes.
Nothing too exciting, it's just a simple SwiftUI app
that lets the user enter their favorite number.
This is currently stored locally.
And I want to use Firebase to store this in the Cloud.
There are two main steps to connect your iOS
app to Firebase, adding your app to the Firebase Console
and installing the SDK.
To create a new project, head over to firebase.com
and then click on the Go To Console button.
On this screen, you will see an overview of all the Firebase
projects you've created.
I want to create a new project.
So I'll click the Add Project button.
Before we go any further, I want to take a moment
to explain the difference between projects and apps.
So the most important thing to know about a Firebase project
is that you can connect one or more apps to it.
All apps in the same Firebase project use the same resources.
That means they will access the same Cloud Filestore, Realtime
Database, Firebase Authentication, and Cloud
Storage backends.
And you can view combined analytics data
across all apps in the same project.
This also means that you only have
to configure these products once in the project settings.
And it will take effect for all your apps.
The most common case is that you've
got versions of the same app on different platforms.
Many apps are available on iOS, Android, and the web.
But you might also have multiple iOS apps
that all need access to the same data.
For these scenarios, we recommend
connecting all those apps to the same Firebase project.
This way, your users can access the same data
if they use your app on both their iPhone and the web.
On the other hand, if your apps are different
and you don't need to share data between them,
you should definitely put those in different Firebase projects.
So this is just a high level overview
of how Firebase projects work.
And there is a lot more should know.
For example, did you know that every Firebase project is also
a Google Cloud project?
This makes it super easy to add Google Cloud's
powerful features to your Firebase app.
My colleague, Marina, recently recorded an entire video
about how Firebase projects work and you should definitely
check it out.
The link is in the description below.
So if your Android or web team has already created a Firebase
project for your app, you should probably select that project
and add your app to it.
If you don't see the project in the Firebase Console,
then go ahead and ask your teammates
to invite you to the project.
But if you're the first one to be
adding Firebase capabilities to your project, well, lucky you.
You get to be the one to create the new Firebase project.
And that's what I'm going to do here.
So after clicking on the App Project button,
you will see this screen where you can
enter the name of your project.
I can type a name.
But you might notice this dropdown box beneath the name.
This gives you the option to add Firebase
to an already existing Google Cloud project
that you have access to.
The Google Cloud project will retain
all its current capabilities while adding
in the new services needed for Firebase.
So if you're working on a project that has already
been set up through Google Cloud,
make sure you pick that and don't create a new Firebase
project.
Me, I'm creating a brand new project.
So I'll type in a new name.
You can see that it is giving me this ID
string that updates as I type.
This is the project ID, which is a globally unique identifier
for your project across Firebase and Google Cloud.
I click on the Continue button.
And now I get to choose whether or not to add
Google Analytics to my project.
Enabling analytics will allow me to use
a number of useful features, such as A/B testing, running
Firebase predictions, and getting crash-free user
reports.
If I don't enable analytics, I won't
be able to use those features.
Enabling is totally optional.
But I don't want to miss out on those cool features.
So I will keep analytics enabled.
Since I decided to enable analytics for my project,
I will have to associate this project with an account.
Now, an account isn't an account like a Gmail account
or anything and that.
It's really just a folder of analytics projects.
It's mostly there for organizational purposes.
Some developers like to have separate accounts
for each project.
But I like to keep mine in one folder.
So I'll pick this now.
I can now proceed to the next step,
which is to actually create the new Firebase project.
Firebase works in the background to set up
the project and provision all the required resources.
And after a moment or so, I have a shiny new project
to work with.
I'll click on Continue, which will take me
to the project overview page of the Firebase Console.
All right, so this is my new Firebase project.
But it is still a bit empty here since I haven't yet
added any apps to it.
So I will click on the iOS+ icon here.
And now there are a bunch of fields for me to fill out.
First, I need to enter the bundle idea of my app.
You can find this in Xcode by selecting
the route of your project in the project navigator
and then clicking on a target.
Go to the General tab, and you will find the bundle identifier
in the identity group.
In my case, it is def.peterfriese.favorites.
So I will copy this and then head back
to the Firebase Console and paste it
into the Apple bundle ID field.
The next two fields are optional.
Feel free to give your app a nickname so it's easier
to find it later.
The App Store ID is used by Firebase Dynamic Links
to redirect users to your App Store page.
And you can always fill this in later in the project settings.
Let's move on to the next step to download this plist file.
This file contains a bunch of configuration settings
that the Firebase SDK will later use to connect to your Firebase
project.
So I'll download this file and then
drag it into my Xcode project.
I usually drop it right beneath the assets catalog, like so.
It's important to make sure that this file is exactly named
Google services info.plist.
So if there is a number in the file
because you download one earlier,
just rename the file right in Xcode.
If you look at this file, you can
see that it contains a bunch of settings
that the Firebase SDK needs to configure itself
correctly and connect to the Firebase project
we just created.
For example, here is the idea of my project,
the Cloud storage bucket I might use,
some OAuth client IDs, and so on.
None of this is secret.
But you should really only share this file with your team.
For example, if you're making an open source demo
app that you want to share publicly,
you'll want to make sure other people who try out your project
use their own version of this file
to connect to their own Firebase project.
So that was the first step, creating a Firebase project
and adding your app to it.
Back in the Firebase Console, we can
see that the next step is to add the Firebase SDK to your iOS
project.
Swift Package Manager is the preferred way
to add Firebase to an iOS project.
But you can still use CocoaPods or a zip file distribution
if your team hasn't made the transition to SPM yet.
Let me copy the package URL and then go back into Xcode
and add the Firebase package to my project.
There are a couple of ways to do this.
For example, you can go to the package dependencies
of your project in the project settings or use the File menu.
I'll use the context menu of the project
and then select Add packages.
In the dialogue that appears, I paste the URL
to the Firebase SDK package into the search field.
And now I can see the readme of the Firebase SDK.
Using the dependency rule, I can now
define which version of the Firebase SDK
I'd like to use in my app.
For production apps, I would recommend
using the up to next major version setting.
Xcode will automatically populate a lower and upper
bound for the version.
This will make sure you always get the latest
stable version of the Firebase SDK
up to the next major version.
Click on Add Package to let Xcode download the packet
and its dependencies.
This might take a moment depending
on your network speed.
Once this is finished, Xcode will
present a list of all the Firebase
libraries that are available.
For now, I will pick just Firebase Analytics and Firebase
Analytics Swift.
Keep in mind that Xcode will add these to the active target
only.
So if you've got more than one target in your project,
you will have to add them to the other targets manually.
The next step is to configure Firebase.
So let's head back to the Firebase Console
and see what we need to do.
So here you can see the code that is required for a SwiftUI
or UI kit with Swift.
And if your app is written in objective-C
or if you just happen to really like square brackets,
there's a code snippet for that as well.
My app uses SwiftUI.
So I'll go ahead and copy this chunk of code
here and then head back over to Xcode.
So my app makes use of the new Swift UI application lifecycle
that Apple introduced a while ago.
The main difference is that apps using this new lifecycle
don't have an app delegate anymore.
But when I paste the code I just copied,
you will notice that this is literally an app delegate.
But not to worry.
Apple has also provided us with a way
to use an app delegate with this new lifecycle model.
All we have to do is register our app delegate
using the Add App Delegate Adapter.
If you'd like to learn more about this,
check out these two blog posts I wrote about the SwiftUI
lifecycle and what it means for our Firebase.
With this in place, we can finish the flow
and console and then launch the app
to verify we've completed all the required steps.
Once the app has started, we can see some output
in Xcode's log console.
You should see one line saying, analytics version so-and-so
started.
And then soon after, another one saying, analytics collection
enabled.
If you forgot to add the googleservicesinfo.plist file,
Firebase will issue an error saying,
could not locate configuration file googleservicesinfo.plist
and then crash.
Yeah, that file is so important that we'd rather
not launch the application than just
try to work without the file.
Now that you've connected your app to Firebase,
you can start using Firebase services in your app.
But first, you might need to link the respective module
and import it into your source code file.
So let me give you a quick example
and show you how you can use analytics
to track when a user visits a specific screen in your app.
Firebase has a really cool SwiftUI view modifier
that makes this super easy.
To use it, we first need to add the Firebase Analytics Swift
library.
Now, you will remember that I already
did this in the beginning.
But just for the sake of the argument,
let me remove it and then add it again
so you can see how it is done.
Next, I go back into the view I want to track and import
Firebase Analytics Swift.
Once that's done, I can go to my view implementation
and apply the analytics screen view modifier to my screen.
The next time this view appears, the Firebase Analytics SDK
will send a screen view event to analytics.
Before running the app again, I will enable debug mode
for analytics so we can see the events in the debug view.
Once the app is running again, let's
go to the Firebase Console and check the analytics debug view.
And sure enough, here is the screen view event.
And when I click on the Firebase screen parameter,
I can see the screen name that I provided in my source code.
Sweet.
All right.
So now that you've added Firebase to your app,
you can start using all the other services
that Firebase offers.
For example, you can store, query,
and sync data from the Cloud using Cloud Filestore
or horizontally scaling document-based NoSQL database
in the Cloud.
Or you could use Firebase authentication to easily assign
end users using sign in mechanisms such as sign
in with Apple, Google sign in, or email and link
authentication.
In this video, we had a quick look at analytics.
But it can do a lot more.
For example, you can use it to record custom events and user
properties and then combine this with Remote Config
to run A/B tests or personalize your app.
Or you can add Crashlytics to your project
to pin down hard to track crashes and increase
the stability of your app and much, much more.
If you like this kind of video, subscribe to our channel
because there are a bunch of other videos about Firebase
and iOS coming out in the next couple of weeks.
Have fun creating.
And I will see you soon on another episode
of Firebase fundamentals.
[AUDIO LOGO]