LoginSignup
4
1

More than 5 years have passed since last update.

Save & Read Data from Firebase - IOS Swift 4

Last updated at Posted at 2018-05-24

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.Screen Shot 2018-05-24 at 1.41.26 PM.png
after that please click on the project and copy the Bundle Identifier.Screen Shot 2018-05-24 at 2.00.55 PM.png

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

Screen Shot 2018-05-24 at 1.47.38 PM.png

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.

Screen Shot 2018-05-24 at 1.51.49 PM.png

after you see this page , you can create a project by clicking on Add ProjectScreen Shot 2018-05-24 at 1.53.40 PM.png

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).Screen Shot 2018-05-24 at 1.55.57 PM.png

To add firebase to our IOS app click on Add Firebase to your IOS appScreen Shot 2018-05-24 at 1.57.52 PM.png

now paste the Bundle Identifier that we copy from our xcode project in the IOS bundle ID field and click registerScreen Shot 2018-05-24 at 3.04.19 PM.pngafter that we have to download the GoogleService-info.plist and drag it into our Xcode project. after that click NextScreen Shot 2018-05-24 at 3.05.45 PM.png

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.

Screen Shot 2018-05-24 at 3.08.33 PM.png
after that click next and you will see below screen.
Screen Shot 2018-05-24 at 3.10.15 PM.png

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)Screen Shot 2018-05-24 at 3.13.21 PM.png

after open the project. Go to AppDelegate.swift Import Firebase and add below code.Screen Shot 2018-05-24 at 3.14.45 PM.png

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

Screen Shot 2018-05-24 at 3.17.22 PM.png

click on Realtime Database and choose start in locked mode.
Screen Shot 2018-05-24 at 3.18.55 PM.png

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.

Screen Shot 2018-05-24 at 3.20.33 PM.png

save. Now we have finished setting up.

3. Save Data into Firebase</Screen Shot 2018-05-24 at 3.28.58 PM.png

Now open our ViewController and write below code

Screen Shot 2018-05-24 at 3.24.26 PM.png

  • 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
Screen Shot 2018-05-24 at 3.28.58 PM.png

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!

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1