[MUSIC PLAYING]
PETER FRIESE: Hi there, iOS developers.
Interested in adding the Firebase platform to your app?
Well, you've come to the right place.
Let's get started with Firebase on iOS
in this episode of "Firecasts."
So there are two main parts to getting the Firebase
platform up and running--
adding your app to the Firebase console,
and installing the SDK.
Let's go over these one at a time.
To add your app to the Firebase console,
we first need to visit the console over at this URL here.
Depending on when you're watching this video,
the UI might look slightly different.
But the general concepts should remain the same.
Now, when you first visit the Firebase console,
you'll see a few options here as to what you can do.
Depending on your situation, you might
see a list of existing projects that
are already using Firebase.
This button down here takes you into a read-only version
of the Firebase console for Flood-It!,
an actual mobile game that's out in the real world.
I think it's a really good way to get to know Firebase and see
what kind of reporting you can get
from an actual app with real data.
To start a new project, as you might have guessed,
I'll be selecting Add Project on this friendly white button
up here.
But before we go further, let me take a moment
to explain the difference between projects and apps.
A project consists of one or more apps.
All apps in the same project use the same Cloud Firestore
and cloud storage for Firebase back ends.
And you can view combined analytics data
across all apps in the same project.
You can also use features like Firebase Cloud
Messaging, or in-app messaging, to talk to all of them at once.
You don't have to, but you can, which is sometimes convenient.
So if you've got versions of your app
on different platforms, such as Android, iOS, and the web,
you'll want to connect each of those platform-specific apps
with the same Firebase project.
That will give you some nice cross-platform benefits.
Your users can access the same data
if they switch back and forth between the iOS and Android
versions of your app, dynamic links
will work across both platforms, and you
could send one notification to all versions of your app,
and so on.
The other hand, if your projects are logically
independent of each other, you should put those
in different Firebase projects.
Generally, you should ask yourself
if your applications are going to use the same data
and configuration.
If they do, it's a good idea to connect them
to the same Firebase project.
Apps that don't share the same data and configuration
should be connected to different Firebase projects.
In particular, if you're building a white label app,
you should set up separate Firebase projects
for each of your customers to keep
everything nicely separated.
So if your Android or web team has already
created a Firebase project and connected their app,
you should probably just select that,
and add your iOS app to it.
But if you're the first one to add Firebase capabilities
to your app, you get to be the one to create the new Firebase
project.
So I am going to create a new project here.
I can give it a name.
But notice, I also have this dropdown box beneath the name.
This gives me the option to import an existing Google
project.
If you've previously created a Google project
by visiting the Google Cloud console,
you probably want to choose this import option.
This will keep all of the capabilities of your existing
project, while adding in all of the new services
needed for Firebase.
I'm starting a brand new project,
so I'll type in a new name.
Notice there is a chip just below the project name, which
will get updated as I type.
This is the project ID which needs to be unique.
By clicking on the Continue button,
I proceed to the next step, which
will give me the opportunity to choose
whether I'd like to use Google Analytics for my project
or not.
As you can see, choosing Analytics
will enable a number of cool features, such as A/B testing,
predicting user behavior, and crash-free user reports.
Un-selecting analytics will disable all those features.
I'll stick with the default and proceed to the next step.
At this point, you need to configure
the connection between your Firebase project and Google
Analytics.
If you're an existing user of Google Analytics,
you can either select one of your existing accounts
or create a new one.
An Analytics account is a way to group
together several different Firebase projects
into one place.
It's basically like a folder.
Don't let the word "account" throw you off too much.
You can have lots of different Analytics accounts associated
with the same Google account.
Some companies like to create a different account
for each different Firebase project.
Others like to put several projects into the same account.
Either way is fine.
Go with whatever makes the most sense to you.
And if you want more information,
check out the Help Center article linked
in the description below.
In my case, I don't have an existing Analytics account,
so I am going to do that first.
In case you decided to create a new account,
or you didn't have an existing one,
you will see this screen that lets you configure Analytics.
First, you'll have to choose the location of your organization,
as this might have an influence on the terms
and conditions which apply to your business location.
I am based in Hamburg, so I'll select Germany.
I am going to stick with the default settings
for sharing analytics data, and then
accept these terms of service.
But you go ahead and do what's best for your organization,
and then click Create Project.
With that out of the way, I can now click Create Project
and watch the magic of my project being created.
Once you've selected or created a project,
you're going to want to connect a client app to it.
You can see I have the choice of connecting an iOS, Android,
web, or Unity app here.
And in my case, I am going to select the iOS button.
Now I need to give it my app's bundle
ID, along with a nickname if I want.
You're going to need to add the app store ID eventually
if you want to use features like dynamic links,
but you can also leave this blank for now,
and change it later.
After clicking Register App, you will
see a Download button that lets you download a Google services
info.plist file.
Note that it needs to be named this exactly
so if you get a little one after the name, like I just did,
you're going to need to do a little renaming in Finder.
Next up, drag this file into your Xcode project,
making sure it ends up in your project root folder, like so.
OK, let me go back to the console and hit Next here.
And it's telling me this would be a good time
to install the Firebase CocoaPod.
I'm assuming you know something about CocoaPods,
but if you don't, we happen to have a nice little video
for you to check out.
There is a link in the description below.
CocoaPods uses Xcode workspaces to manage
the dependencies between my project and the pods it uses.
So I'm going to close my Xcode project,
jump into my project directory, and run pod init
to create a basic pod file.
I'll open up the file.
I'm using vi, but feel free to use your favorite text
editor here.
Now, I guess I could uncommit this line
to specify my deployment target of iOS 13 or greater.
Although, it should be noted that,
at the time of this writing, Firebase
is supported as far back as iOS 8 for almost all components.
So let's add a pod to install the Firebase SDK.
It's important to remember that, to keep your app lean and mean,
you should only install the pods for features
that you actually use.
In fact, there is no all encompassing Firebase CocoaPod
that installs everything for you.
You're going to need to pod install
each individual feature.
You can find a full list of CocoaPods in our documentation.
For starters, I'm just going to add the Firebase Analytics
pod, which includes everything needed
to get Google Analytics for Firebase up and running.
So we'll add pod Firebase Analytics here, and we're done.
Let's quit out of our editor.
Next up, I'll make sure my project is closed,
run pod install, and then open up
the generated workspace using the handy xed dot command.
Let's build it to make sure everything compiles OK.
And we can move on to the next step.
OK, it looks like the next step here
is to add some initialization code.
I recommend putting this into your app delegate
didFinishLaunching method.
First things first, let's import Firebase.
Note that this is usually the only thing
you'll ever need to import, no matter what I've installed.
We've done some pretty nifty work behind the scenes
to make sure this works properly.
I know this sounds like the exact opposite of my "only
install what you ever need" advice from two minutes ago,
but trust me here, this makes development much easier.
Next up, I'll add the line FirebaseApp.configure()
in my app delegate application:didF
inishLaunchingWithOptions method,
to make sure Firebase gets set up properly.
This configure method will take a look at which Firebase
modules you have installed and initialize them,
grabbing the appropriate constants from their Google
Service info.plist file you dragged in earlier.
To verify everything works as expected,
I quickly run the application on my simulator.
Once started, the application will try
to communicate with Firebase.
And back in the Firebase console,
I'll go to my final step where I can see here
that my app has communicated with the Firebase servers.
Nice.
Congratulations.
You're now up and running with the Firebase platform.
There is a lot of places you can go from here.
You can add sign-in using Firebase Auth,
get your app talking to Cloud Firestore,
or start using Analytics to see how your users are really
interacting with your app.
You can check out all the links in the description below
to get started.
Thanks for watching, and I will see you in the next one.
[MUSIC PLAYING]