区分 iPhone 应用程序崩溃和被杀死
你好,我计划开发一个简单的 iPhone 游戏。我希望能够区分真正的崩溃和用户杀死应用程序(通过双击主屏幕并长按它来杀死它)。
有人可以解释一下当用户通过多任务栏杀死应用程序时到底会发生什么吗?
Hello I am planning to develop a simple iPhone game. I would like to be able to distinguish between a genuine crash, and the user killing the app ( by double tapping home screen and long-clicking it to kill it) .
Can someone please shed some light on what exactly happens when the user kill the app through the multitasking bar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的应用程序处于后台并在用户终止它时暂停,它将不会收到任何通知。这占大多数情况。
如果您的应用当前正在后台运行(只有非常特定类别的应用可以执行此操作),那么它会收到
applicationWillTerminate
。事实上,苹果非常清楚,你应该在进入后台之前保存任何相关数据。看看 此(“响应应用程序终止”一章):
编辑:
关于“说抱歉”的事情......
你当然可以在下一次发布时做到这一点。只需将一个密钥存储在 NSUserDefaults 中,并在应用程序进入后台时将其删除(我希望这一切听起来对您来说很熟悉,否则请查看 UIApplicationDelegate 协议)。
当应用程序启动时,您检查密钥;如果存在,则用户没有关闭该应用程序;如果应用程序不存在,那么用户至少将应用程序移动到后台并且没有遇到任何突然终止...
If your app is in the background and suspended when the user kills it, it will receive no notification. This accounts for the majority of cases.
If your app is currently running in the background (there are only very specific categories of apps that can do that), then it receives
applicationWillTerminate
.Indeed, Apple is very clear as to the fact that you should save any relevant data before entering the background. Have a look at this (chapter "Responding to Application Termination"):
EDIT:
about the "saying sorry" thing...
you can certainly do that on the next launch. simply store a key in
NSUserDefaults
and remove it when the app enters the background (I hope all this sounds familiar to you, otherwise look into the UIApplicationDelegate protocol).when the app starts up, you check the key; if it is there, then the app was not closed by the user; if the app is not there, then the user at least moved the app to the background and did not experience any sudden termination...
对于 iOS6 及更高版本有一种方法可以做到这一点。 状态恢复的副作用是它会删除恢复期间发生崩溃或用户手动终止应用程序时的状态。您可以利用这一点来检测用户手动杀死该应用程序。
来自
以下代码假设您已经有一个名为
_didCrashInLastSession
的用于崩溃检测的BOOL
。获取此值的方法有多种,例如这个第三方库。在您的代码中调用方法[self getLaunchType]
来查看您正在处理的启动类型并采取相应的操作。将以下内容放入您的 AppDelegate.m 中:更新:至少有一个第 3 方 SDK 打破了这一技术:Urban Airship。
For iOS6 and later there is a way to do this. A side effect of State Restoration is that it will delete the state when there is either a crash during restore or a user manually kills the app. You can use this to your advantage to detect a user manually killing the app.
From the documentation:
The following code assumes that you already have a
BOOL
for crash detection called_didCrashInLastSession
. There are different approaches for getting this value such as this 3rd party library. In your code call the method[self getLaunchType]
to see which type of launch you are dealing with and act on that accordingly. Put the following in your AppDelegate.m:Update: At least one 3rd party SDK breaks this technique: Urban Airship.
您也可以通过您的设备来完成此操作。
将您的设备连接到您的机器。
运行 xcode 并转到管理器。
在那里选择您的设备和设备日志。
在那里您还可以查看应用程序或游戏的崩溃日志。
You can do it through your device also.
Connect your device to your machine.
Run xcode and go to organizer.
There select your device and device logs.
There you can also see crash logs of your app or game.