Customizing Firebase Crashlytics Crash Reports in Swift iOS

Mastering Crash Reports: Swift Strategies with Firebase Crashlytics

Vikram Kumar
4 min readDec 13, 2023

Introduction

Crashes are an inevitable aspect of app development, but identifying and fixing them promptly is crucial for providing a seamless user experience. Firebase Crashlytics, part of the Firebase suite, offers a robust solution for tracking and analyzing crashes in iOS applications. In this article, we will not only explore how to integrate Crashlytics into a Swift iOS project but also delve into customizing crash reports to gain deeper insights into app issues.

How to Add Crashlytics to Your Swift Project

Before customizing crash reports, it’s essential to set up Crashlytics in your Swift project. Follow these steps to integrate Crashlytics into your Xcode project:

Firebase Project Setup:

  1. Create a Firebase project on the Firebase Console.
  2. Follow the on-screen instructions to add your iOS app to the Firebase project.

Install the Firebase SDK:

  1. Open your Xcode project and navigate to the root directory.
  2. Run pod init in the terminal to create a Podfile.
  3. Add the following line to your Podfile:
pod 'Firebase/Crashlytics'

4. Run pod install to install the Firebase Crashlytics SDK.

Initialize Firebase in Your App:

  • Open your AppDelegate.swift file.
  • Import Firebase at the top of the file:
import Firebase

Add the following code in the didFinishLaunchingWithOptions method:

FirebaseApp.configure()

Enable Crashlytics:

  • Open your Xcode project and navigate to the Firebase Console.
  • In the Crashlytics section, enable Crashlytics for your iOS app.

Set up Xcode to automatically upload dSYM files

To generate human readable crash reports, Crashlytics needs your project’s debug symbol (dSYM) files. The following steps describe how to configure Xcode to automatically produce your dSYMs, process them, and upload the files whenever you build your app.

  1. Open your project’s Xcode workspace, then select its project file in the left navigator.
  2. From the TARGETS list, select your main build target.
  3. Click the Build Settings tab, then complete the following steps so that Xcode produces dSYMs for your builds.
  4. Click All, then search for debug information format.
  5. Set Debug Information Format to DWARF with dSYM File for all your build types.
  6. Click the Build Phases tab, then complete the following steps so that Xcode can process your dSYMs and upload the files.
  7. Click add > New Run Script Phase.
  8. Make sure this new Run Script phase is your project’s last build phase; otherwise, Crashlytics can’t properly process dSYMs.
  9. Expand the new Run Script section.
  10. In the script field (located under the Shell label), add the following run script. This script processes your project’s dSYM files and uploads the files to Crashlytics.
"${PODS_ROOT}/FirebaseCrashlytics/run"

11. In the Input Files section, add the paths for the locations of the following files:

${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}

${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Resources/DWARF/${PRODUCT_NAME}

${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Info.plist

$(TARGET_BUILD_DIR)/$(UNLOCALIZED_RESOURCES_FOLDER_PATH)/GoogleService-Info.plist

$(TARGET_BUILD_DIR)/$(EXECUTABLE_PATH)

Build and Run:

  • Build and run your app on a simulator or a physical device.
  • Add code to your app that you can use to force a test crash.
  • Verify that the initial Crashlytics setup is successful.
// SwiftUI
Button("Crash") {
fatalError("Crash was triggered")
}

// UIKit
let arr = [0]
let _ = arr[1]

How Crashlytics Works

Firebase Crashlytics works by automatically capturing and reporting crashes in your iOS app. Here’s an overview of the process:

Initialization: When your app launches, Crashlytics initializes and begins monitoring for crashes.

Crash Detection: If a crash occurs, Crashlytics captures the crash data, including the stack trace and other relevant information.

Data Transmission: Crashlytics securely transmits the crash data to the Firebase servers.

Dashboard Presentation: The crash data is presented on the Firebase Crashlytics dashboard, allowing you to analyze and understand the issues affecting your app.

Now that Crashlytics is integrated into your project, let’s explore how to customize crash reports to gain additional insights and context.

Customizing Crash Reports

1. Import Firebase Crashlytics

First, make sure you’ve imported Firebase Crashlytics in your Swift file where you want to customize crash reports.

import FirebaseCrashlytics

2. Set User Information

Adding user information to crash reports can be valuable for understanding the impact of crashes on specific user segments. You can set user information like this:

let userID = "12345"
Crashlytics.crashlytics().setUserID(userID)

3. Log Custom Events

Logging custom events can provide additional information about user interactions or specific scenarios leading to a crash. Use the following code to log custom events:

Crashlytics.crashlytics().log("Custom event: User tapped a specific button.")

4. Attach Custom Keys

Attaching custom key-value pairs to crash reports allows you to filter and search for specific information. For example:

Crashlytics.crashlytics().setCustomValue("Premium", forKey: "UserType")

// Crashlytics.crashlytics().setCustomValue(UserManager.shared.user.email, forKey: "Email")
Crashlytics.crashlytics().setCustomValue("example@email.com", forKey: "Email")
Crashlytics.crashlytics().setCustomValue("John Doe", forKey: "Name")

5. Test Your Implementation

Before releasing your app, it’s crucial to test your Crashlytics implementation. Simulate a crash in your code and verify that the custom information appears in the Firebase Crashlytics dashboard.

Conclusion

Customizing Firebase Crashlytics crash reports in your Swift iOS app can significantly enhance your ability to identify, understand, and resolve issues. By adding context through user information, custom events, and key-value pairs, you empower yourself to streamline the debugging process and deliver a more stable app to your users.

Happy coding!

Thank you for reading until the end. Before you go:

--

--

Vikram Kumar

I am Vikram, a Senior iOS Developer at Matellio Inc. focused on writing clean and efficient code. Complex problem-solver with an analytical and driven mindset.