Swift: Natively Detect If App Has Crashed

Swift: Natively Detect if App has Crashed

Thanks to a little help from @RyanCollins, I was able to solve the problem myself. The function applicationWillTerminate in the App Delegate only runs when the app closes properly. The code to natively detecting an app crash looks like this.

Globally Defined Variables

let crashedNotificationKey = "com.stackoverflow.crashNotificationKey"
var crashedLastTime = true

App Delegate

func applicationWillTerminate(application: UIApplication) {
crashedLastTime = false
prefs.setBool(crashedLastTime, forKey: "crash")
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
crashedLastTime = prefs.boolForKey("crash")
if crashedLastTime == true {

crashedLastTime = false
prefs.setBool(crashedLastTime, forKey: "crash")
NSNotificationCenter.defaultCenter().postNotificationName(crashedNotificationKey, object: self)

} else {

crashedLastTime = true
prefs.setBool(crashedLastTime, forKey: "crash")

}

return true
}

Root View Controller

override func awakeFromNib() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "crashedAlert", name: crashedNotificationKey, object: nil)
}

func crashedAlert() {
let alert = UIAlertController(title: "The app has crashed!", message: "Sorry about that! I am just a 17 year old highschooler making my first game!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "It's cool bro.", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}

How to use swift to detect app crash?

I don't think there is currently a way to do this. When your app crashes it ceases to function and all processes stop. Usually any crash and diagnostic data is sent via the user settings panel.

Edit: There is an helpful post here: https://stackoverflow.com/a/8242215/4891259

Hope that helps.

How to use swift to detect app crash?

I don't think there is currently a way to do this. When your app crashes it ceases to function and all processes stop. Usually any crash and diagnostic data is sent via the user settings panel.

Edit: There is an helpful post here: https://stackoverflow.com/a/8242215/4891259

Hope that helps.

Detect app crashed during load / last time it was run?

Make 2 functions in your AppDelegate.m file:

void HandleException(NSException *exception) {
NSLog(@"App crashing with exception: %@", exception);
//Save somewhere that your app has crashed.
}

void HandleSignal(int signal) {
NSLog(@"We received a signal: %d", signal);
//Save somewhere that your app has crashed.
}

Then in your -(BOOL)application:didFinishLaunchingWithOptions before anything else put:

NSSetUncaughtExceptionHandler(&HandleException);

struct sigaction signalAction;
memset(&signalAction, 0, sizeof(signalAction));
signalAction.sa_handler = &HandleSignal;

sigaction(SIGABRT, &signalAction, NULL);
sigaction(SIGILL, &signalAction, NULL);
sigaction(SIGBUS, &signalAction, NULL);

My swift based Mac OS app crashes on launch on other machines

I'd recommend that you attach the debugger to the application on your non-dev machine. This can be achieved by running lldb <path to app> and then process launch when the prompt is ready again. This will work as a standard debugger and should help you understand your error more completely and debug your issue.

There is no catch all for this sort of issue as to why it wouldn't run on another machine and so using the debugger to get information on the issue is the best choice in this situation.

UIView snapshot crashes in iOS app running natively on Apple Silicon

We also had this crash, suspected it had something to do with using a Metal layer in our app. Couldn't figure out the exact cause, but the issue seems to be resolved in macOS Monterey.



Related Topics



Leave a reply



Submit