Book Image

Firebase Cookbook

By : Houssem Yahiaoui
Book Image

Firebase Cookbook

By: Houssem Yahiaoui

Overview of this book

Do you feel tired just thinking or even hearing about backend technologies, authentication or the tedious task of deployment? Firebase is here to change the way you develop and make your app a first-class citizen of the cloud. This books takes a solution based approach by providing you recipes that would help you understand the features of Firebase and implement them in your existing web or mobile applications. We start-off by creating our first Firebase application and integrating its services into different platforms and environments for mobile as well as web applications. Then we deep dive into Real-time Database and Firebase Storage that allows your users to access data across various devices with realtive ease. With each chapter you will gradually create the building blocks of your application from securing your data with Firebase Rules to authenticating your users with O-Auth. Moving along we would explore modern application development techniques such as creating serverless applications with Firebase Cloud Functions or turning your traditional applications into progressive apps with Service workers. Finally you will learn how to create cross-platform mobile apps, integrate Firebase in native platforms, and learn how to monetize your mobile applications using Admob for Android and iOS.
Table of Contents (15 chapters)
Firebase Cloud FireStore

Integrating Firebase in iOS applications

Integrating Firebase our iOS application involves adding the Firebase package similar to any other package you have probably worked with in the past.

Getting ready 

In order to create and integrate Firebase within your application, you will need to have a MacBook Pro or one of Apple's computer variants so that you can follow the upcoming steps; you will also need to install Xcode.

How to do it...

In order to create an iOS application, open Xcode and follow the given steps:

  1. Create a new project or open your already created project (Figure 17):
Figure 17: Xcode project opening/creation
  1. In our case, we're about to start a new project called firebasecookbook. It's going to be based on the Xcode single-view application project template.
In our application--or when it comes to an example provided in this book regarding Firebase and iOS--we will work with Swift instead. It's just a personal preference, but you can use the one that suits you best.
Figure 18: Xcode project creation

Don't forget to copy that Bundle Identifier, because we will need it in the next step. In this case, our Bundle Identifier is hcodex.firebasecookbook.

  1. Go to your Firebase dashboard and click on the Add Firebase to your iOS app button. After clicking on it, you will get a configuration model with some steps that will guide you in your Firebase iOS integration (Figure 19).
Figure 19: Xcode project creation
  1. Remember that bundle id or Bundle Identifier? Copy and paste that ID in its designated place and add a nickname for your application if you wish. Click on the REGISTER APP button.
  2. Next, we will need to download a special plist file called the GoogleService-info.plist file. This file will have all the necessary metadata that will be included in your project (Figure 20).
Figure 20: Firebase GoogleService-info.plist download
  1. Now simply copy and paste that file into your project (Figure 21):
Figure 21: Firebase GoogleService-info.plist in our application.
  1. After we have finished the file download and file integration, let's just install some dependencies. In this process, we will use CocoaPods as our package manager. Head directly to your project using your terminal:
      ~ cd project-directory
~/project-directory ~> pod init
To download and install CocoaPods on your macOS development machine, please follow the steps mentioned on the official website:

After you initialize your project with CocoaPods, you will find a newly created file named Podfile.

The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects.

  1. Now, you will need to edit the Podfile using your favorite code or text editor and add the following line:
      pod 'Firebase/Core'
  1. Now, save the file, go back to your terminal, and write down the following command:
       ~/project-directory ~> pod install 

This command will download and install all the required libraries and modules that Firebase needs to fire up within your application.

  1. Now, instead of your regular project, open another special project extension, as the following command shows:
       ~/project-directory ~> open project-name.xcworkspace
  1. We're one step behind. Now, in your application, go directly to your AppDelegate and simply import Firebase using the following code snippet:
      import Firebase
  1. Now, in the didFinishLaunchingWithOptions method, add the following code:

Congratulations! You've successfully integrated Firebase with your iOS application.