0

For an OS X app with persistent state, is there anyway to programatically detect whether the last time the application was open, it crashed, or closed unexpectedly? (So I can perform some actions to ensure the application is in a consistent state)

user3601148
  • 175
  • 1
  • 9
  • 1
    Possible duplicate of [Detect App Crash on Launch](http://stackoverflow.com/questions/33844313/detect-app-crash-on-launch) – kelin May 01 '17 at 08:47

1 Answers1

0

You can use NSSetUncaughtExceptionHandler as described here.

    NSSetUncaughtExceptionHandler { exception in
        print("Exception: \(exception.name) 
                          \(exception.reason!)
                          \(exception.description)")
        print(exception.callStackSymbols)
    }

The code I am using is...

NSSetUncaughtExceptionHandler {
    exception in
    ExceptionManager.handle(exception)
}

And the ExceptionManager class includes...

class ExceptionManager {

    class func handle(exception: NSException) {
        var exceptions = [ExceptionItem]()
        if let items = NSUserDefaults.standardUserDefaults().arrayForKey("UncaughtExceptions") as? [NSData] {
            for item in items {
                if let exception = NSKeyedUnarchiver.unarchiveObjectWithData(item) as? ExceptionItem {
                    exceptions.append(exception)
                }
            }
        }

        let newItem = ExceptionItem(NSDate(), exception)
        exceptions.insert(newItem, atIndex: 0)
        if exceptions.count > 5 { // Only keep the last 5 exceptions
            exceptions = Array(exceptions[0..<5])
        }

        var items = [NSData]()
        for e in exceptions {
            let item = NSKeyedArchiver.archivedDataWithRootObject(e) as NSData
            items.append(item)
        }
        NSUserDefaults.standardUserDefaults().setObject(items, forKey: "UncaughtExceptions")
    }
Michael
  • 8,891
  • 3
  • 29
  • 42
  • Unfortunately, I have already seen that answer somewhere else, and it does not work for me. Get an error: a c function pointer can only be formed from a reference to a func or a literal closure – user3601148 Feb 08 '16 at 05:13
  • Can you post the code you are using. I am using this technique to log crashes with Swift 2, and it works for me. I don't have the exact code I'm using with me, but I should be able to post it in a couple of hours. – Michael Feb 08 '16 at 05:14
  • it's just simply calling another function I wrote in the swift class inside the NSSetUncaughtExceptionHandler. Your code works fine, but I want to set and save a value via another function which gives me the error stated above. – user3601148 Feb 08 '16 at 05:21
  • NSSetUncaughtExceptionHandler { exception in print("Exception: \(exception.name)") writeToPlist(true) } – user3601148 Feb 08 '16 at 05:26
  • I have updated what I'm doing. The main difference is I'm using a separate class and class method. Does your need self.writeToPlist? Or just take the simple path and copy what I'm doing. – Michael Feb 08 '16 at 11:09