Step-by-Step Guide on Firebase Crash Reporting

Objective
To get  complete crash-report whenever your app crashes. In Simple words, Firebase Crash Reporting will let you know the reason of crash  along with line number in your view controller or any class.

firebase

 

 

 

 

Prerequisite
I’m assuming you have a project, where you want to add Firebase Crash Reporting.

First thing first, You need Cocoapod installed on your Mac and a working project (or, any iOS Single View Application).

Step 1. Login into your Firebase Console

screen-shot-2016-12-31-at-2-46-20-am

 

 

Click on ‘Create New Project’  and then pass on relevant information about your app (like, Your App Name and its country).

screen-shot-2016-12-31-at-2-47-32-am

 

 

 

 

 

 

 

Step 2. Now, you are on Firebase Dashboard

screen-shot-2016-12-31-at-2-53-06-am

As you can see there are several Firebase service, but we are quietly interested in Firebase Crash Reporting

screen-shot-2016-12-31-at-2-55-38-am

 

 

Next, they will ask you to choose your platform (Obviously, it’s iOS)

screen-shot-2016-12-31-at-2-57-24-am

 

 

 

 

When you get into it, there is form like window pops up.

screen-shot-2016-12-31-at-3-02-58-am

 

 

 

 

 

 

 

 

 

Where, you have to pass on bundle id of your app. Rest two are optional which you can ignore.

So, From I can get my Bundle Id ?
Answer is, You have to move on to you app and select your Project > General and there you go Bundle Identifier under Identity section.

screen-shot-2016-12-31-at-3-02-37-am

 

Say, I have com.innovationm.FirebaseCrashReportingDemo  and it could be different (as, its Projects Dependent).

Step 3.
Now, you have to add Google Services Info.plist file which has all the credentials and private identifiers which Firebase will use for internal purpose (Doesn’t make sense, still you have to add as Google Developer is asking you to do that 😀 ).

screen-shot-2016-12-31-at-3-08-36-am

 

 

 

 

 

 

 

 

 

So, drap and drop the GoogleService-Info.plist in your xcode.

Suggestion : If your file name starts with  GoogleService-Info(1).plist  then, you should omit (1) from the file name. As, Firebase expects only GoogleService-Info.plist as its configuration file name. Else, it will cause confusion and pops you runtime error.

Step 4.
Now, they will ask you to install pod of Firebase Core (further, Firebase Crash). As, these are the dependencies which we need in this project.
In simple words, we are going to use Firebase so we need to integrate firebase framework (or, library) in your project.
So, Cocoapod will do it for us.

screen-shot-2016-12-31-at-3-42-20-am

 

 

 

 

 

 

 

 

 

 

Open Terminal (CMD+Space and type Terminal)  and type cd [Path_Of_Your_Project] and return key.
There is a shortcut to get the path of project directory.

Tips : Select your Project Directory in Finder and drag-drop to Terminal itself. It will give you the path of dropped directory.

Step 4.1 :
cd [PATH_NAME]
screen-shot-2016-12-31-at-4-04-22-am

 

 

 

 

 

 

 

 

Step 4.2 :
Type Pod init and return key.
screen-shot-2016-12-31-at-4-05-25-am

 

 

 

 

 

 

 

 

Step 4.3 :
That makes a Podfile in root directory of your project.
screen-shot-2016-12-31-at-4-05-58-am

 

 

 

 

 

 

Step 4.4 : And, Open it in any Text Editor (as, I’m addict of Sublime so, I’ll use it). And, paste the pod file name and save the file.
screen-shot-2016-12-31-at-4-06-58-am

 

 

 

 

 

 

 

Alert : Make sure, you add these two lines in between Line 6 and 12 (as per image).

Step 4.5 :
Type pod install and install the dependencies which you have mention.
screen-shot-2016-12-31-at-4-36-39-am

 

 

 

 

 

 

 

 

Step 4.6 :
Close terminal and open your .workspace in your project directory.
screen-shot-2016-12-31-at-4-21-10-am

 

 

 

 

 

 

Alert : If you open .xcodeproj file then you won’t get pod dependencies in your project. And, it will throw error that dependencies files are not found.

Step 5.
Ultimately, we have a project which has Firebase Crash Reporting .
Now, configure Firebase in AppDelgate.swift
screen-shot-2016-12-31-at-3-45-09-am

 

 

 

 

 

 

 

 

 

 

So, AppDelegate.swift is like
screen-shot-2016-12-31-at-4-37-28-am

 

 

 

Step 6.
Next part is bit tricky, as we have to write a piece of script in Build Phase.
First, go to your Project Screen.
screen-shot-2016-12-31-at-4-40-25-amAnd, choose Build Phase and click on + Button (just below Capability)

screen-shot-2016-12-31-at-4-40-42-am
There you have options to choose.
screen-shot-2016-12-31-at-4-42-54-am

 

 

 

Next, we have a blank script screen with some default text.
screen-shot-2016-12-31-at-4-43-06-amSo, we have to add these lines in script box.

# Replace this with the GOOGLE_APP_ID from your GoogleService-Info.plist file
GOOGLE_APP_ID=1:my:app:id

# Replace the /Path/To/ServiceAccount.json with the path to the key you just downloaded
"${PODS_ROOT}"/FirebaseCrash/upload-sym "/Path/To/ServiceAccount.json"

Though, we have to replace bold text i.e GOOGLE_APP_ID and /Path/To/ServiceAccount.json with ‘projects’ value.

First, we will get Google App Id from Info.plist which we have downloaded (i.e. GoogleService-Info.plist).
screen-shot-2016-12-31-at-4-48-45-am

 

 

 

 

 

And, Next we need ServiceAccount JSON file which we’ll get it from Firebase Console.

Click on Setting (Gear Button which is on right side of OverView Button) >  Service Account (last Tab) > Crash Reporting > Generate Private Key Button.
screen-shot-2016-12-31-at-4-50-24-amDownload the file and rename whatever you want. Say, mine is firebasecrashreport.json .

And, paste in root folder of your project. After doing this, drag and drop it to xcode project.

screen-shot-2016-12-31-at-4-53-31-am

 

 

 

 

 

 

 

So, My Script would be like,

# Replace this with the GOOGLE_APP_ID from your GoogleService-Info.plist file
1:xxxxxxxxx:ios:xxxxxxxxxxx=1:my:app:id

# Replace the /Path/To/ServiceAccount.json with the path to the key you just downloaded
"${PODS_ROOT}"/FirebaseCrash/upload-sym "FirebaseCrashReportingDemo/firebasecrashreport.json"

FirebaseCrashReportingDemo is the name of my project and respective to it firebasecrashreport.json, which makes my absolute path such.

Congratulations, you’re done with Firebase Crash Reporting.

Step 7.
Testing and Logging in Firebase Console.
Step 7.1 : Add a line of statement which make a crash.

fatalError() will do crash it for you.

1

 

 

 

 

 

 

 

Step 7.2 : Launch your app and it will crash.
Step 7.3 : Comment the crash Line.
Step 7.4 : Launch your App (It will send the log to Firebase Console).
After 2-3 Minutes, it will log into Firebase Console.

 Initially, it will be like
firebase

 

 

 

 

Click on Error, and it will move you to Crash Reporting Panel.
screen-shot-2016-12-31-at-5-16-14-amIt say, we are crashing at line number 17 for ViewController.swift screen.

Is it true ?
screen-shot-2016-12-31-at-5-16-45-am

 

 

 

 

 

 

 

 

Yes, we have a buggy code at line number 17 and that makes sense.
screen-shot-2016-12-31-at-5-16-14-amscreen-shot-2016-12-31-at-5-32-36-amAdditional Feature

You can send custom logs to Firebase console using FIRCrashMessage(“TYPE_YOUR_MESSAGE”) method.

Further Reference

Leave a Reply