Crash-Time Handler

Set Custom Values Before Sending a Crash

When an application crashes, details about the app’s runtime state are gathered and prepared for submission to Instabug on the next launch. However, what if you want to run some code after generating the report? This is where the onCrashHandler kicks in.

The onCrashHandler hook enables you to run custom code after the crash report has been generated. The information then becomes accessible in the “User Data” section of the dashboard after the next launch, during the didSendCrashReportHandler phase.

The callback is executing at crash-time, it must be async safe, which means that Objective-C code cannot be used. A C function can be registered

Here's an example of the code to be called on a crashing session:

void onCrashedHandler(IBGCrashReportWriter *writer) {
    writer->addUIntegerElement(writer, "total_memory", [NSProcessInfo processInfo].physicalMemory);
    CFTimeInterval timeInSeconds = CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970;
    writer->addUIntegerElement(writer, "timestamp", (unsigned long)timeInSeconds);
}

It should also be set in the Instabug's Crash Reporter:

[IBGCrashReporting setOnCrashHandler:onCrashedHandler];
IBGCrashReporting.onCrashHandler = onCrashedHandler

Checking if the Last Session Crashed

If you want to check whether the last session was a crashing session, you can use the following API. This API returns a boolean value indicating whether the app crashed in the previous session.

IBGCrashReporting.didLastSessionCrash

This property is particularly useful if you need to take specific actions based on whether the last session crashed or not.


Exporting Crash Data for Additional Insights

Beyond determining if the last session ended in a crash, you can capture detailed information about the crash itself. The didSendCrashReportHandler API lets you export various metadata about the crash, including:

  • Crash Type: the error type including Fatal, non-fatal, OOM..
  • Error Code: The specific error code associated with the crash.
  • Error Description: A textual description providing further context about the error.
  • User Attributes: User attributes associated with the session.
  • Additional Crash Data: additional metadata added by the onCrashHandler callback
IBGCrashReporting.didSendCrashReportHandler = ^(IBGCrashMetaData *metadata) {
  ...
};
CrashReporting.didSendCrashReportHandler = { metadata in
  ...
}

📘

Note:

  • Any function called within a signal handler must be asynchronous-safe. This means that Objective-C code, among others, is not safe to use in this context.
  • This is enabled through a Feature flag. So, please contact our support team at [email protected] to enable it