An iOS app needs the backend services for performing the various task like storing data in the database, hosting of files, caching data, monitoring etc. Firebase is a cloud-based service that provides most of the server side things for our iOS apps so that we don't have to manage the infrastructure like databases, servers, and server-side things by yourself. Firebase will take care of everything. Firebase provides many useful services that make our life easier, but the most important services are

Remote Database

Remote Config

Analytics

Authentication

Crash Reporting

In this post, we will see how to get started with Firebase and how to configure the database and authentication services provided by Firebase. Let's get started

Setup Firebase

To get started with Firebase, you need to have a Google account. With your Google account, visit https://firebase.google.com/ and log in with your credential. You can go to the Firebase console where Firebase will ask you to Create a Project with your country. The Firebase UI is very friendly and guides you through every step, so you don't need to worry about anything once you enter the console.

Add iOS App

We can then add our iOS app to the project by clicking on Add Firebase to Your iOS App.

Here, we need to specify the Bundle ID of our iOS app, which has to be unique. I have created com.sashikantjagtap.XCTalk for my personal project.

Download Config File

Inthe next step, Firebase creates a file called GoogleService-Info.plist file which we need to download and add it to our Xcode project, just near to Info.plist file. Don't forget to select Copy items if needed and Create folder references.

When you download the config file, drag it to Xcode project and don't forget to select Copy items if needed and Create folder references.

There is a little bug in Xcode where you need to check that the GoogleService-Info.plist file has the correct target membership, as we have to explicitly add it for some reason.

Register Firebase With the App

The final step is to configure our iOS app to use Firebase. We can modify the AppDelegate.Swift file to use Firebase in our app.

That is everything needed and everything is well documented in the setup guide for Firebase. I have followed all the steps for the demo app. Now that, the interesting part is to use the Firebase and its services inside our App. At the end, you will see your app in the Firebase console.

Firebase Database

Now that we have done all the setup for the iOS app to use Firebase Database and Authentication, let's try to create the database and add a dummy message to test that the connection is working.

Test the Connection

Let's add the following code in the AppDelegate.Swift file just below where we have configured Firebase, i.e. FirebaseApp.configure().

Probably not- you will get a permission denied error in the Xcode console, which indicates that we need to allow permission for our app to write to Firebase. So head over to the Firebase console and select Database Service and Rules and change it to something like this:

{
"rules": {
".read": true,
".write": true
}
}

We are basically allowing everyone to write to the database for now. If you run the app, you can see that your message has been logged in the Cloud database. Yay !

Add Data

We probably need to add more complex stuff to the database, not the just hello message. Let's consider your app has a UI to add messages and we are adding messages and sender info to Firebase. We can create a child database and add it using code that looks something like this:

We can do the more complex operations on the database, but for now, we will carry on with the next section: authentication of users.

Firebase Authentication

Firebase is also used for authentication services like login and registration. Firebase handles authentication painlessly by providing easy methods. Before diving into the authentication service code, we need to enable authentication from the Firebase console. From the console, select the Authentication tab and SIGN-IN METHOD and select the method we want to use for sign-in users. I will select the email/password method.

Let's consider our app has a UI to enter username and password for registration and login.

Registration

We can easily register users using the Firebase method createUser, like this:

You can see how easy it is to use Firebase for registering users to an app.

There are many other services that Firebase provides for iOS apps, but we will have to stop here, as you might have a basic understanding of how Firebase works in general.

Conclusion

Using cloud-based services like Firebase, we can simplify a lot of iOS app development activities and develop applications rapidly in the fast-paced market. However, you have to find the balance ofhow much you depend on the cloud. What are your experiences with using Firebase in production? Let me know in the comments. Thanks for reading.