Establishing a Firebase Reference in Swift 3

Firebase is a no-SQL, database-as-a-service tool that allows you to quickly create apps without having to worry about building a backend. It’s a really nice tool because it allows you to spend your time creating your app without worrying about writing a database server and forming HTTP transactions. Many apps just need to store simple data structures and file data on the cloud and Firebase makes this fast and easy!

Creating a Firebase Project

Start by creating a new project in XCode, be sure to save your bundle ID because you will need it when creating the Firebase Project.

Screen Shot 2016-11-04 at 11.30.12 AM.png

Now navigate to https://firebase.google.com and log in. Then click on Go to Console where you can create a new project. Name it and click Create Project.

Screen Shot 2016-11-04 at 11.34.43 AM.png

This will take you to your Firebase Dashboard. You want to click Add Firebase to your iOS App. Now you just have to follow the steps for adding Firebase to your iOS App!

Setting Permissions

Once you finished setting up your project on Firebase you need to change the default permissions to be able to add data to your database since the database is private by default.

Navigate to the Database menu item in your Firebase project and then click on the Rules tab.

A quick fix to make your project public is to change the default rules from “auth != null” to “auth == null”. This will make your database public so you can add data to it.

Screen Shot 2016-11-04 at 12.12.20 PM.png

Cocoa Pods

If you are new to Cocoa Pods, they are a way to install packages for an iOS app. For our Firebase app, we would to install the Swift Firebase library.

To install Cocoa Pods (for Xcode 7 & 8) just type the following in your terminal.

sudo gem install cocoapods

Now cd into your XCode project folder and type:

$ pod init

This command creates a Podfile in your directory. A Podilfe contains the configuration for the Cocoa Pods in your project. Open up that Podfile with an editor and assert that following is correct in your Podfile:

  • Your App Name is under target <App Name> where <App Name> is your app name
  • Under “# Pods for <AppName>”
    • add pod ‘Firebase/Core’
    • add pod ‘Firebase/Database’

Screen Shot 2016-11-04 at 11.44.10 AM.png

This lets Cocoa Pods know which libraries you would like to install and for which project. Now you can save the file and type pod install.

App.xcworkspace

When installing the Firebase libraries a file named <My App>.xcworkspace where <My App> will be the name of your app. You should use this file for opening your XCode project from now on because it contains all of the installed libraries that we need. So if you still have the other project open in XCode, close it an open the new .xcworkspace project file

Creating a Firebase Reference

In order to have a reference to our Firebase database in our app we need to call a Firebase configure method that is called every time our app opens. This is best done in AppDelegate.swift.

Open AppDelegate.swift and import Firebase and put the line FIRApp.configure() in the App Delegate application(didFinishLaunchingWithOptions) method.

Screen Shot 2016-11-04 at 11.57.02 AM.png

Now navigate to the View Controller where you would like your firebase reference and add the following code.

Screen Shot 2016-11-04 at 12.18.36 PM.png

This code creates a variable of type FIRDatabaseReference and sets a value (“Hello World”) in your database. It’s that easy! Now if you run the app, the ViewDidLoad() method will be called and push “Hello World” to your Firebase DatabaseB!

You can now navigate to the database menu item on the Firebase project dashboard and see the new data right away!

Screen Shot 2016-11-04 at 12.21.19 PM.png

Learning Firebase

You now have an app with a reference to your Firebase database! You can easily create an app and call the Firebase library methods for adding data to your database. Firebase also provides great documentation for interacting with its system. You can learn more about using Firebase at https://firebase.google.com/docs/.