有没有办法判断一个iOS应用程序是否意外关闭?(崩溃,强制关闭) [英] is there a way to tell if an iOS app is closed unexpectedly?(crash, force close)
问题描述
如果应用程序上次意外关闭,我想在应用程序启动时执行某些操作。(崩溃,强行关闭),我该怎么说?
I want to do something on app start if the app is closed unexpectedly last time.(crash, force close), how can I tell it?
推荐答案
我通过在应用启动时和NSUserDefaults中将 BOOL
设置为 YES
来实现此目的应用程序进入前台。
I do this by setting a BOOL
to YES
in NSUserDefaults on app startup and when the app enters the foreground.
当应用程序进入后台时,我将此相同值设置为否
。
I set this same value to NO
when the app enters the background.
在的开头didFinishLaunchingWithOptions
(在将标志设置为YES之前),我检查标志当前是否设置为是
。如果它是 YES
,那么我知道应用程序不正常崩溃。
At the start of didFinishLaunchingWithOptions
(before setting the flag to YES), I check if the flag is currently set to YES
. If it is YES
, then I know the app crashed uncleanly.
但是,这不会检查应用程序是否在后台被杀死(由操作系统或用户)。
However, this does not check if the app was killed in the background (by the OS or the user).
你可以通过设置一个在后台(由操作系统或用户)关闭一个力应用程序进入后台时标记并在应用程序进入前景时清除标记。然后在 didFinishLaunchingWithOptions
方法中检查此标志。如果设置了这个标志,那么你就知道你的应用程序在后台被杀了。
You can handle a force close while in the background (by the OS or the user) by setting a flag when the app enters the background and clearing the flag when the app enters the foreground. Then check for this flag in the didFinishLaunchingWithOptions
method. If this flag is set then you know your app was killed while in the background.
请记住,在后台关闭的力量不应该被认为是意外关闭。这实际上是非常期待的。
Keep in mind that a force close while in the background should not really be considered an "unexpected close". It's actually quite expected.
这篇关于有没有办法判断一个iOS应用程序是否意外关闭?(崩溃,强制关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!