📦 SwiftyLaunch Modules
📈 AnalyticsKit
Crash Detection

Automatic Crash Detection

AnalyticsKit (SwiftyLaunch Module) - Automatically Detect Crashes

If you have FirebaseKit (AuthKit + DBKit) selected during project generation, SwiftyLaunch will add Crashlytics support to your generated app, which will automatically detect any crash and send a crash event to PostHog.

Walkthrough

When the is launched, we check if the app crashed during the last execution. If it did, we send the crash report to PostHog and display an in-app notification to the user.

App.swift
class AppDelegate: /* ... */ {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        // ...
        if Crashlytics.crashlytics().didCrashDuringPreviousExecution() {
            Analytics.capture(
                .error,
                id: "previous_app_execution_crashed",
                longDescription: "Check the Crashlytics Logs in the Firebase Console to see the stack trace.",
                source: .crash
            )
            // ... show in-app notification
        }
        // ...
    }
}
 

Crash related In-App Notification Example

The crash event will show up in your PostHog dashboard, but the full stack trace will be available in the Firebase Console.

Crash Event in the PostHog Dashboard

PostHog Crash Event

Crash Event and Stack Trace in the Firebase Console

Firebase Crash Event Img 1

Firebase Crash Event Img 2

Try it yourself

To try out crash detection, install your app on a real device, stop the Xcode debugger, and then launch the app again. (Crashlytics doesn't work in the simulator or when the app is launched from Xcode.)

Go to the Developer tab in the App, scroll down and press on Crash App. The app will crash, and the next time you launch it, the app will show the user an in-app notification and upload the crash report to Firebase in the background.

Crash App Button

Behind the scenes

After each crash, the Crashlytics SDK automatically sends a report to Firebase that a crash has happened which includes the stack trace. In order for the stack trace to be human-readable, the dSYM file is required. It acts as a "translator", allowing Firebase to convert the cryptic crash report into a human-readable stack trace. (So you can see function names, etc.)

The dSYM file is generated every time your app is built, and it is stored in the DerivedData folder. SwiftyLaunch includes a script that automatically uploads the dSYM file to Firebase after each build, so you don't have to worry about it. The script is follows official Crashlytics documentation (opens in a new tab) and can be seen in the Build Phases section of your app target.

dSYM Upload Script

Missing dSYM in the Crashlytics Dashboard

Sometimes you may see in the Crashlytics Dashboard that a crash has occured, but you cannot access the stack trace, and you see a message that the dSYM file is missing. This is because the dSYM file is not yet uploaded to Firebase.

The upload will happen automatically in the background, you just need to wait a few minutes. Can take longer especially when the app is being executed in a development environment.

Missing dSYM in Firebase Console

If for some reason (probably due to Xcode being a buggy mess) the script fails, you can manually upload the missing dSYM file.

Manually uploading the dSYM file

First, search for the dSYM file on your system. Copy-paste this into your terminal:

mdfind -name .dSYM | while read -r line; do dwarfdump -u "$line"; done

Find the dSYM file that matches the UUID of the one missing in the Crashlytics dashboard. Copy the .dSYM file to Desktop, and then zip it.

dSYM File Search Terminal

In the Crashlytics Console, in the dSYMs section, click on Upload dSYM and select the zipped dSYM file.

dSYM Manual Upload