Skip to content

Latest commit

 

History

History
67 lines (55 loc) · 2.31 KB

File metadata and controls

67 lines (55 loc) · 2.31 KB

NSExceptionKt for Firebase Crashlytics

Installation

First make sure you have set up Crashlytics (v9.3.0 or above).

After that add and export the Kotlin dependency to your appleMain source set.

kotlin {
    iosArm64 { // and/or any other Apple target 
        binaries.framework {
            isStatic = true // it's recommended to use a static framework
            export("com.rickclephas.kmp:nsexception-kt-core:<version>")
        }
    }
    sourceSets {
        appleMain {
            dependencies {
                api("com.rickclephas.kmp:nsexception-kt-core:<version>")
            }
        }
    }
}

Now in your Xcode project add the NSExceptionKtCrashlytics dependency and update your Firebase configuration logic with a call to NSExceptionKt.addReporter:

import Firebase
import NSExceptionKtCrashlytics
import shared // This is your shared Kotlin module

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
    ) -> Bool {
        FirebaseApp.configure()
        NSExceptionKt.addReporter(.crashlytics(causedByStrategy: .append))
        return true
    }
}

That's all, now go and crash that app!

Implementation details

The Firebase Crashlytics iOS SDK has a limited API, which means it (currently) doesn't support the concept of caused by exceptions.

NSExceptionKt provides 3 different strategies to handle this:

public enum CausedByStrategy {
    /// Causes will be ignored, only the main Throwable is logged as a fatal exception.
    case ignore
    /// Causes are appended to the main Throwable and logged as a single fatal exception.
    case append
    /// All causes are logged as non-fatal exceptions before the main Throwable is logged as a fatal exception.
    case logNonFatal
}

Crashlytics only stores a single fatal exception, so the final Kotlin termination won't be recorded.