OK guys, So This time we are going to talk about Firebase.
What is Firebase?
Base on Google Official Document ,Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business. But to make it simple you can just think about firebase as a Service provider. It provides many service like , Realtime database , Authentication , Hosting , Cloud Storage ... etc. and this time we are going to use firebase database to store or save and fetch data from our application.
Getting Started
1. Create Xcode Project
You have to set up your Xcode Project with Firebase in order to make it work. So first of all , let's us create a Xcode Project. I create a project and name it as FirebaseSampleProject.
after that please click on the project and copy the Bundle Identifier.
Good job. Now let's go to create firebase project.
2. Create Firebase Project
Now we finish create our xcode project so now it's time to play with firebase , now let's go to Firebase official website. (below is the link)
https://firebase.google.com
at this point I think everybody already has a google account. If not please create and log in. Then click on GET STARTED or GO TO CONSOLE.
after you see this page , you can create a project by clicking on Add Project
fill everything and click CREATE PROJECT . It will take a little time to set up our project. After it finish setting up please click continue then it will route you to our project (please see below picture).
To add firebase to our IOS app click on Add Firebase to your IOS app
now paste the Bundle Identifier that we copy from our xcode project in the IOS bundle ID field and click registerafter that we have to download the GoogleService-info.plist and drag it into our Xcode project. after that click Next
at this stage we have to use CocoaPod to install Firebase library. you have to include two pod file which are pod "Firebase/Core" and pod "Firebase/Database" see below picture.
after that click next and you will see below screen.
ok at this stage we have to go back to our xcode project but since we are using cocoapod so we have to use workspace instead. (see below picture)
after open the project. Go to AppDelegate.swift Import Firebase and add below code.
after that run our xcode project. if you follow all the steps, I am sure there will be no problem.
now let go back to firebase website and click on Develop -> Database
click on Realtime Database and choose start in locked mode.
now click on RULES tap and change read , write to true. Don't worry about it since we are just doing a testing. changing it to true so it doesn't need authentication when we do the request from our application.
save. Now we have finished setting up.
3. Save Data into Firebase</
Now open our ViewController and write below code
- first We create the instance of FirebaseDatabase by giving databaseRefer = Database.database().reference()
- then we are saving the data into our firebase database by using child & childByAutoId & setValue.
- Child : the field name
- childByAutoId : automatically assign the id when created
- setValue : value of the field name
run the code below and go back to firebase database. you will see those value in it.
4. Read Data from Firebase
add below code to our ViewController
so to read data from firebase we are using DatabaseHandle object to handle the reference from databaseReference. The value will be printing out to the console.
YES! THAT'S IT WE ARE FINISHED!