Real-time iPhone app in Swift 2 and Firebase

You must have seen Facebook, Twitter and all similar social apps and wondered how live it is when someone comment, like or tweet you get that thing at that moment. Well its lot easy to develop similar things using Firebase. Exciting? Lets develop our first app!

Setting up Firebase

1. Create account on Firebase
Sign up to Firebase After sign up Firebase will create one free app for you with its unique URL. We will use this URL to write and read data for our app.

2. Create iOS application
Create one Single View Application using swift in Xcode, say “HelloFirebase”.

3. Install Cocoapods
Open terminal and write below command,
- $ sudo gem install cocoapods
If you are new to cocoa pods, you can read about it at cocoa pods website[] where they say that - “CocoaPods is the dependency manager for Cocoa projects. It has thousands of libraries and can help you scale your projects elegantly.”
If you already have cocoa pods make sure you have latest version else update it.

4. Install Firebase using Cocoapods
Open terminal and execute below commands,
- $ cd /Users/peopletech/Desktop/HelloFirebase
- $ pod init
- $ open -a Xcode Podfile

Once it open Podfile, add below line in it after target 'HelloFirebase' do
pod 'Firebase'

Go back to terminal and execute these two commands,
- $ pod install
- $ open HelloFirebase.xcworkspace

It will open our app with firebase apis included in pods. We are all set now to use Firebase in our app!

Using Firebase

1. Create “bridging header file”
To use pod frameworks written in objective-C in our swift project you would require “bridging header file” where we can import the required classes. Once imported it can be used across all swift class in our application.
Please follow Apple’s guidelines on how to create bridging header files:

It will create bridging header file for app - HelloFirebase-Bridging-Header.h. Import Firebase class in it,
- #import <Firebase/Firebase.h>

2. Design view
Design our view in storyboard and add appropriate constraints.

Add IBOutlets for textfiled and label, also add IBAction to Send button.
@IBOutlet weak var _textField: UITextField!
@IBOutlet weak var _label: UILabel!

@IBAction func sendToFirebase(sender: AnyObject) {}

3. Writing to Firebase
First of all we need to create reference to our database URL endpoint to write something firebase.
Add variable in our ViewController.swift,
var myRootRef: Firebase?

To initialise myRootRef, Write below line of code in viewDidLoad() of our ViewController.swift
myRootRef = Firebase(url: "")

Here given URL is our database node's endpoint URL where we want to write value.

Now lets add code to write data back to the firebase which user is entering into the textField,

@IBAction func sendToFirebase(sender: AnyObject) {
// Write data to Firebase

4. Listening to Firebase node
Just add below line of code in our viewDidLoad() of ViewController.swift and see the magic,

myRootRef!.observeEventType(FEventType.Value) { (snapshot: FDataSnapshot!) -> Void in
self._label.text = "Value: \(snapshot.value) "

Thats it! Now what? Lets run our first app for Firebase!
Youtube link:

Isn’t it cool? Write down in comments to me if you have any queries.
Happy Coding...!!

By jay nanavati   Popularity  (1070 Views)