LAURENCE MORONEY: Hey, what's up, everybody?
Laurence Moroney here from the Firebase team.
And welcome to another episode of "Firecasts."
On today's episode, I'm going to show you
how to sign in with Google using Firebase Auth on iOS.
And from that, you'll see how to sign in with other providers,
too.
You'll be up and running with this feature
in just a few minutes.
So let's get started.
First, we'll have to make sure that your development
environment is set up.
To get started with Firebase in iOS, create a new app in Xcode.
Make it a single-view app, and give it a product name.
Click through to create it.
And here you can see it's on my desktop.
Using Terminal, go to the Project folder
and do the pod init command.
You'll see a pod file is created.
Edit this pod file to add the required Firebase components.
I'm just using Firebase/core for this demo.
Once you're done, run the pod install command,
and the components will be downloaded and added
to an xcworkspace.
Work with this from now on.
Don't use the Xcode proj file.
Remember the bundle identifier.
You're going to need that on the Firebase console.
On here, choose an existing project
or add a new one by specifying its name.
Once you've created your project,
you can add an app to it.
To add an iOS app, just specify the bundle ID.
A plist file will be downloaded to your machine.
You can drag and drop this into your App folder.
You now have everything you need to get
started with Firebase on iOS.
Great.
Now that you have a working development environment set up,
the next steps will be to build a simple app that lets
you sign in with Google on iOS.
You'll import the required files,
create simple views, set up the required custom URL schemes,
and then finally code a simple view with a sign-in button that
signs into Google and tells your app
the ID of the signed-in person.
Before you can start coding your iOS app,
you'll need to ensure that it's associated with your Firebase
project.
On the console, you'll see it.
Look for the little iOS icon here.
You'll also need to ensure that you've enabled the correct Auth
providers.
To do this, enter the Auth section, select Sign-in Method,
and enable the desired one.
Here you can see I have Google enabled as my sign-in method.
Now let's take a look at the code in Xcode.
If you've set your project up correctly,
you'll see a bunch of pods in the Pod folder.
On your Info tab, you need to ensure that your URL schemes
are set up properly.
You can see them here under URL types.
The first one can be gotten from the plist file
that you got on the Firebase console.
You can see it in your project here.
Pick the reversed client ID for the URL scheme,
as you can see here.
The second one is just the bundle name.
And that's easy to remember.
The storyboard should have a view representing
your sign-in button.
You can't see this in the designer,
but put a view on there and take a look at the custom class
to ensure that it's the GID sign-in button.
For signing out, just use a generic button.
And in the view controller, you can add an action
to the button for Sign-out Clicked.
Also add outlets for both buttons,
and specify that the view controller is a GID sign-in
delegate.
So on the view controller itself on load,
we do all the setup, specifying the client
ID and the delegates for the different sign-in methods.
Once the user signs in, you'll get the didSignInForUser
callback.
If there are no errors, it gets the credentials from the user
and turns them into a Firebase Auth credential.
This is then used to sign into Firebase.
And once it's successfully signed in,
we'll get a user object back.
We then generate a simple alert box,
picking the display name from that user object
and showing it.
For signing out, we have an action
which is called signOutClicked.
And on that, we create a Firebase Auth object and call
signOut on it.
Let's see this in action.
The app is running on the emulator.
And we can see the button Chrome has
been created for us at runtime.
When we sign in, we're taken to the accounts.google.com page
where I can sign in with my familiar Google account.
I specify my username and password and then sign in.
I'm asked for permissions for what to access.
And if I allow that, I'm signed in, returned to the app,
and then the flow happens.
My name is extracted, and it's displayed in the alert dialog.
It's really as simple as that.
If I click Sign Out, I get signed out successfully.
But if I sign in again, I've already
gone through the credential flow,
so I just sign directly into the app.
And that's Firebase Auth-- sign-in made easy.
There you go.
You've just built your first app that signs in with Google
on iOS in just a few minutes.
Now you're ready to personalize your app
or add other service providers such as Facebook, GitHub,
and more.
And that's it for this "Firecast."
Please leave your questions in the comments below,
or reach out on Twitter and G+ using the #askFirebase hashtag.
And don't forget to hit that Subscribe button.
I'm Laurence Moroney.
And thanks for watching.
[MUSIC PLAYING]