Implementing Firestore in your iOS Application

Implementing Firestore in your iOS Application

Hello Friends,

Are you facing problems in implementing Cloud Firestore Database in your iOS Application.
If yes ??

It is no more problem for you now. Today we are going to discuss configuration of firestore in your iOS application.

First we have to know what is Cloud Firestore and what are the benefits.

  • Firestore is the no sql cloud database
  • Stores and Sync Data
  • Firestore provides offline storage of data
  • Firestore is the Native iOS SDK
  • Easy to use.
  • It gives real time updates.

Firestore based on Collections and Document. Under document it has data which we can get in JSON or Dictionary form. In the below example users is the collection and YTNQzg0JZBRB6qaS5ZCS is the document id generated by firestore. You may create your own document. User data will always store in documents and you may create nested collection inside documents.

Figure 1.0

It is easy to configure Firestore. We have to follow fixed number of steps to integrate it into your application.

Step 1:
First of all we have to register our in firebase console. To do that we have to go to the firebase.google.com
On the top right of the page there is a button Go to console. As shown below.

Figure 1.1

Step 2:
Click on Add project.

Figure 1.2

Step 3:
Add Project name whatever you want. Press Continue. 

  • Click continue in Google Analytics Page.
  • Choose default firebase account.

Your project is added in the firestore. Now click on iOS icon present inside your project in the dashboard and follow Step 4.

Step 4:
Please be careful in filling these details. These are the details associated with your Xcode project.

  • Enter your bundle id same bundle id is used in Xcode bundle id.
  • Fill App nickname which is optional.
  • Enter App Store ID Which is optional as well

Click Register app.

Figure 1.3

Now we have GoogleService-Info.plsit After registering App.

  • Download this plist file for your Xcode Project.
  • Click Next.
  • Go ahead and click next.

Step 5:
Click on Database  and create Database and select appropriate option. I select Production mode. Click Done

Step 6:
Now you have to set rules to access. Select Rules and update allow read, write: If false to allow read, write: If true

Here is the complete snippet

rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } }

And Publish.

Now you are done with configuring your application on firestore. Now you are ready to dive into Xcode.

Xcode Portion

  • Open Xcode and create a single view application.
  • Enter your project name and hit enter
  • Click on your project > General. Here you enter the same bundle Id you enter in the firestore project.

Drag and drop the same GoogleService-Info.plist which you downloaded from firestore. The name should be GoogleService-Info.plist. Remove any other character if there is any like in GoogleService-Info.plist (1) you have to remove “ (1)” from the name.

Figure 1.4

Now we have to do little coding to create Database.
First we have to create podfile in the project

  • Open Terminal
  • Drag and drop your root project folder in terminal
  • Create pod file in project  “pod init”
  • Include below pods in your podfile
pod 'Firebase/Core'
pod 'Firebase/Firestore'
  • Save podfile and install pod “pod install”

Close your project and open xcworkspace.

Figure 1.5

Real Coding Starts now.

Now we have to configure Firestore setting in Xcode

Open AppDelegate.swift Include these lines to configure firestore.

First we have to import FirebaseFirestore and import Firebase on the top.

we have to move to didFinishLaunching method and include

FirebaseApp.configure()

AppDelegate look like this

we are going to enter data in cloud firestore with few lines of code.

First get database reference from firestore by writing

let db = Firestore.firestore()

Now you can set your by creating Collection and document. And data will add inside document

let userDoc = db.collection(“users”).document(“userDocument”) //Set your data inside document userDoc.setData([ "first": "Steve", "last": "Austin", "born": 1965 ]) { (error) in if let err = error { print(err.localizedDescription) }else { print("Data Updated successfully..!!!") } }

Here is your code look like

Now clean and run your code.

You can see your data in cloud firestore

Thank You..!!!!

Please follow and like us:

Leave A Comment

Your email address will not be published. Required fields are marked *