Get Started With Firebase for iOS Apps

DZone 's Guide to

Get Started With Firebase for iOS Apps

Learn how to set up and configure the Firebase service to simplify tasks in iOS app development, like remote database and authentication.

· Mobile Zone ·
Free Resource

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.

Set Up CocoaPods

The next step is to configure CocoaPods that comes with Firebase.

We basically need the Firebase Database and Authentication mechanism, so add those to the Podfile.

pod 'Firebase'
pod 'Firebase/Core'
pod 'Firebase/Database'
pod 'Firebase/Auth'

Then install the pod using the pod install command.

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().

let db = Database.database().reference()
db.setValue("Hello Firebase")

Now run the app and see if you can write to Firebase Database.

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:

let messagesDB = Database.database().reference().child("Messages")

        let messageDictionary : NSDictionary = ["Sender" : Auth.auth().currentUser!.email as String!, "MessageBody" : messageTextfield.text!]

        messagesDB.childByAutoId().setValue(messageDictionary) {
            (error, ref) in
            if error != nil {
            else {
                print("Message saved successfully!")

Let's see how those values look like in the Firebase Database.

Retrieve Data

We can also retrieve the data from database using Firebase. Let's see how to retrieve the sender and messages that we just added to the database.

let messageDB = Database.database().reference().child("Messages")

        messageDB.observe(.childAdded, with: { snapshot in

            let snapshotValue = snapshot.value as! NSDictionary
            let text = snapshotValue["MessageBody"] as! String
            let sender = snapshotValue["Sender"] as! String

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.


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

Auth.auth().createUser(withEmail: emailTextfield.text!, password: passwordTextfield.text!, completion: { (user, error) in
            if error != nil {
            else {

                print ("Registration Successful!")

Firebase will take care all the registration stuff internally.


Similar to registration, Firebase recognizes a registered user and allows them to sign in using the signIn method:

Auth.auth().signIn(withEmail: emailTextfield.text!, password: passwordTextfield.text!, completion: { (user, error) in
            if error != nil {
            else {

                print("login successful")


Firebase also has the method to log out users. It has the signOut method, which can be used like this:

do {
      try Auth.auth().signOut()
        catch {
            print("error: there was a problem logging out")

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.


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.

firebase, ios, mobile, mobile app development, swift

Published at DZone with permission of Shashikant Jagtap , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}