我已经四处搜寻,但是还没有找到一个答案,该答案并未将我引向第3方服务。我不需要任何复杂的东西,只是为了保存一个值,NSUserDefaults所以当该应用程序下次打开时,我可以显示一条警报,指出该应用程序已崩溃。
NSUserDefaults
谢谢。
感谢@RyanCollins的一点帮助,我能够自己解决问题。“ applicationWillTerminate应用程序委托”中的功能仅在应用程序正常关闭时运行。用于本地检测应用程序崩溃的代码如下所示。
applicationWillTerminate
全局定义的变量
let crashedNotificationKey = "com.stackoverflow.crashNotificationKey" var crashedLastTime = true
应用程序委托
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 }
根视图控制器
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) }