区分 iPhone 应用程序崩溃和被杀死 [英] Distinguish between an iPhone App crashing and being killed

查看:23
本文介绍了区分 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.

推荐答案

如果您的应用程序在后台并在用户杀死它时暂停,它将不会收到任何通知.这占大多数情况.

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.

如果您的应用当前在后台运行(只有非常特定类别的应用可以做到这一点),那么它会收到 applicationWillTerminate.

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.

确实,Apple 非常清楚你应该在进入后台之前保存任何相关数据.看看 this(响应应用程序终止"一章):

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"):

即使您使用 iOS SDK 4 及更高版本开发应用程序,您仍必须做好准备,以免您的应用程序在没有任何通知的情况下被终止.用户可以使用多任务 UI 显式终止应用程序.此外,如果内存受到限制,系统可能会从内存中删除应用程序以腾出更多空间.如果您的应用程序当前处于挂起状态,系统会终止您的应用程序并将其从内存中删除,恕不另行通知.但是,如果您的应用程序当前正在后台运行(换句话说,未挂起),系统会调用您的应用程序委托的 applicationWillTerminate: 方法.您的应用程序无法通过此方法请求额外的后台执行时间.

Even if you develop your application using iOS SDK 4 and later, you must still be prepared for your application to be killed without any notification. The user can kill applications explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove applications from memory to make more room. If your application is currently suspended, the system kills your application and removes it from memory without any notice. However, if your application is currently running in the background state (in other words, not suspended), the system calls the applicationWillTerminate: method of your application delegate. Your application cannot request additional background execution time from this method.

关于说对不起"的事情......

about the "saying sorry" thing...

您当然可以在下一个发布时这样做.只需在 NSUserDefaults 中存储一个键,并在应用程序进入后台时将其删除(我希望这一切对您来说听起来很熟悉,否则请查看 UIApplicationDelegate 协议).

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...

这篇关于区分 iPhone 应用程序崩溃和被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆