Getting Started With CocoaPods – Install Your First Dependency: Firebase 3

Category: Swift
Getting Started With CocoaPods – Install Your First Dependency: Firebase 3

This short article is a follow up to Getting started with CocoaPods - Update Ruby Version and Install CocoaPods on Mac OS X 10.11 El Capitan. In the previous article we resolved problem with Ruby version and then installed CocoaPods. Now we’ll look at using CocoaPods to add Firebase SDK to your Xcode project and start using this amazing product by Google.

Firebase is a mobile platform that helps you quickly develop high-quality apps. Firebase compliments of great features that your application needs: Realtime Database, Authentication, Storage and many more.

Before we get started installing Firebase 3 SDK via CocoaPods there are several commands that we will be using. Let’s take a look!

To make sure that CocoaPods is installed simply use the following command: pod. In couple of seconds you will see the list of available commands. To get familiar with basic terminology, you should know that:

Central Repository – also known as Trunk, where all libraries and frameworks are located

For each Xcode project you will have a Podfile where you will list all dependencies of your project. You can add some later and re-run pod install command to download new libraries, so called pods.

To create a Podfile for your project open up Terminal and navigate to your projects root directory - cd your_projects_root_directory. Inside that directory run the following command - pod init. This command will auto-create a Podfile for you.

The next step is to edit your Podfile and list the pods you would like to use. When you are done save the file and in Terminal (in the same directory where your Podfile is located) issue this command: pod install. The installation will start and CocoaPods will download the pods that you “requested” in the Podfile and all other dependencies that your required pods depend on. The installation process will take some time.

Install Firebase

To install Firebase go to www.firebase.google.com. You can access Firebase with your Google account. Click “Go to console” at the right top corner of the page and hit Create Project button.

Next you will have to enter your project name (it could be the same name as your Xcode project, or whatever you want) and select your country. The selected country does not determine the location of your data for Firebase features.

As soon as the project is created you will see the following on the dashboard.

Firebase Dashboard

Select iOS and in the modal window you will have to configure the project.

NOTE: This configuration is specific to Firebase. You might be wondering how to know what lines to add to your Podfile. Refer to documentation of the library/framework either on the developer’s website or on GitHub.

Enter iOS bundle ID which you can find in the General tab for your app's primary target in Xcode. On the next screen you’ll have to download GoogleService-Info.plist and add it to your project (don’t forget to check Destination: Copy items if needed):

Download Google plist for Firebase

Now let’s get to Cocoapods!

As already described in the introductory part, navigate to the root of your project and create a Podfile if you don't have one by the following command from your Terminal: pod init

This will create a Podfile in your projects root directory and now the most interesting part.

Firebase includes several, well, let’s call them components. Firebase Auth, Realtime Database, Storage and so on. If you just insert pod 'Firebase' into your Podfile that you’ll have access to Firebase Analytics by default.

You have to decide which components you would like to use and see respective documentation.

For example to add Firebase Realtime Database to your app ensure that the respective dependency is in your project's Podfile.

In my project I’m going to use Firebase Auth, Realtime Database and Storage, so I have to include all of them. Of course you can add them to your Podfile whenever you need them. My Podfile looks like this:

When you are done updating your Podfile save it (also make sure to quit Xcode) and in your Terminal window run the following command pod install. CocoaPods will make sure to download all of the dependencies and it will also create a new file in your project’s root directory with an extension .xcworkspace.

In order to have access to the frameworks/libraries you have downloaded via CocoaPods you have to use your_project_name.xcworkspace instead of .xcodeproj file.

To import Firebase into your app, open AppDelegate.swift in your project, at the top of the document, after import UIKit import Firebase: import Firebase.

Next to initialize Firebase in your project add FIRApp.configure() in didFinishLaunchingWithOptions function just like this:

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // [START initialize_firebase]
    FIRApp.configure()
…

That’s all!

Conclusion

Now you are ready to start using Firebase in your project and add amazing features that Firebase offers into your app. If you want to learn more about Firebase and how to implement Firebase Auth, Realtime Database and Storage you can watch amazing video tutorials by Lets Build That App on YouTube. Brian showcases the usage of Firebase and builds an exciting, real world application. I admire his passion sharing his knowledge and experience with the world! So watch his tutorials if you really want to take a big step forward into iOS and Swift world. In case of questions or suggestions feel free to send me an email of find me on twitter - @mrgott. Cheers!