Dank eine wenig Hilfe von @RyanCollins zu bedienen, ich war in der Lage, das Problem selbst zu lösen. Die Funktion applicationWillTerminate
im App-Delegaten wird nur ausgeführt, wenn die App ordnungsgemäß geschlossen wird. Der Code zum nativen Erkennen eines App-Absturzes sieht folgendermaßen aus.
global definierte Variablen
let crashedNotificationKey = "com.stackoverflow.crashNotificationKey"
var crashedLastTime : Bool! = true
App Delegierter
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)
}
Werfen Sie einen Blick auf: htt p: //stackoverflow.com/questions/10885313/detect-app-crashed-during-load-last-time-it-was-run –