Force a crash to test your implementation
Add code to your app that you can use to force a test crash.
You can use the following code to add a button to your app that, when pressed, causes a crash. The button is labeled "Test Crash".
SwiftUI
Button("Crash") { fatalError("Crash was triggered") }
UIKit
Swift
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let button = UIButton(type: .roundedRect) button.frame = CGRect(x: 20, y: 50, width: 100, height: 30) button.setTitle("Test Crash", for: []) button.addTarget(self, action: #selector(self.crashButtonTapped(_:)), for: .touchUpInside) view.addSubview(button) } @IBAction func crashButtonTapped(_ sender: AnyObject) { let numbers = [0] let _ = numbers[1] } }
Objective-C
#import "ViewController.h" @implementation ViewController ‐ (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(20, 50, 100, 30); [button setTitle:@"Test Crash" forState:UIControlStateNormal]; [button addTarget:self action:@selector(crashButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } ‐ (IBAction)crashButtonTapped:(id)sender { @[][1]; } @end
Build and run your app in Xcode with the Xcode debugger disconnected.
Click
Build and then run the current scheme to build your app on a test device or simulator.Wait until your app is running, then click Crashlytics.
Stop running the scheme or action to close the initial instance of your app. This initial instance included the debugger that interferes with
Force the test crash in order to send your app's first crash report:
Open your app from the home screen of your test device or simulator.
In your app, press the "Test Crash" button that you added using the code above.
After your app crashes, run it again from Xcode so that your app can send the crash report to Firebase.
Go to the Crashlytics dashboard of the Firebase console to see your test crash.
If you've refreshed the console and you're still not seeing the test crash after five minutes, try enabling debug logging (next section).
Enable debug logging for Crashlytics
If you don't see your test crash in the Crashlytics dashboard, you can use debug logging for Crashlytics to help track down the problem.
Enable debug logging:
In Xcode, select Product > Scheme > Edit scheme.
Select Run from the left menu, then select the Arguments tab.
In the Arguments Passed on Launch section, add
-FIRDebugEnabled
.
Force a test crash. The first section on this page describes how to do this.
Within your logs, search for a log message from Crashlytics that contains the following string, which verifies that your app is sending crashes to Firebase.
Completed report submission
If you don't see this log or your test crash in the Crashlytics dashboard of the Firebase console after five minutes, reach out to Firebase Support with a copy of your log output so that we can help you troubleshoot further.
Next steps
- Customize your crash report setup by adding opt-in reporting, logs, keys, and tracking of non-fatal errors.