从附录A开始吧. 因为理解应用的生命周期是理解开发的基础。
#应用生命周期事件
Your app’s one and only application object (a UIApplication instance, or on rare occasions a UIApplication subclass instance) is created for you as the shared application object by UIApplicationMain, along with its delegate; in the Xcode project templates,this delegate is an instance of the AppDelegate class. The application reports lifetime events through method calls to its delegate; other instances can also register to receive most of these events as notifications.
你的应用唯一的“Apllication"对象(一个UIApplication实例,或者在很少情况下,一个UIApplication子类的实例)由UIApplicationMain为你创建。同时它也创建了“Application”对象的delegate对象。在Xcode项目模版里,这个delegate对象是AppDelegate类的实例。应用程序通过方法调用delegate对象来报告生命周期中的一些事件;其它实例也可以注册来当作notifcation接收大部分事件。
These events, notifying you of stages in the lifetime of your app as a whole and giving your code an opportunity to run in response, are extraordinarily important and fundamental. This appendix is devoted to a survey of them, along with some typical scenarios in which they will arrive.
这些事件非常重要也非常基础,它们告知你的应用当前在整个应用生命周期中所处的阶段从而给你用代码来响应这些阶段的机会。这个附录会借一些这些事件出现的典型场景专门讨论这些事件。
Application States
In the early days of iOS — before iOS 4 — the lifetime of an app was extremely simple:either it was running or it wasn’t. The user tapped your app’s icon in the springboard,and your app was launched and began to run. The user used your app for a while.
Eventually, the user pressed the Home button (the physical button next to the screen) and your app was terminated — it was no longer running. The user had quit your app.
Launch, run, quit: that was the entire life cycle of an app. If the user decided to use your app again, the whole cycle started again.
应用程序的状态集
在在早期的iOS4之前的iOS版本中,app的生命周期超级简单,它不是在运行,就是没运行。用户在屏幕上点应用的图标,应用就启动然后开始运行。用户使用一段时间之后,用户按Home键,然后你的应用就终止了,也就是不在运行状态了。用户退出了你的应用。启动,运行,退出:这就是那时候应用的整个生命周期。如果用户决定再次使用你的应用,整个循环又一次开始。
The reason for this simplicity was that, before iOS 4, an iOS device, with its slow processor and its almost brutal paucity of memory and other resources, compensated for its own shortcomings by a simple rule: it could runonly one app at a time. While your app was running, it occupied not only the entire screen but the vast majority of the device’s resources, leaving room only for the system and some hidden built-in processes to support it; it had, in effect, sole and complete control of the device.
之所以这么简单,原因在于iOS4之前,iOS设备的处理器比较慢。总的来说,系统的资源只够跑一个程序的。(一个应用在运行时占用的不仅仅是屏幕,还包括大量其它资源)。
Starting in iOS 4, that changed. Apple devised an ingenious architecture whereby,
despite the device’s limited resources, more than one app could run simultaneously sort of. The Home button changed its meaning and its effect upon your app: contrary to the naïve perception of some users, the Home button was no longer a Quit button.
Nowadays, when the user presses the Home button to leave your app, your app does not die; technically, the Home button does not terminate your app. When your app occupies the entire screen, it isin the foreground(orfrontmost); when some other app proceeds to occupy the entire screen, your app isbackgrounded and suspended. This means that your app is essentially freeze-dried; its process still exists, but it isn’t actively running, and it isn’t getting any events — though notifications can be stored by the system for later delivery if your app comes to the front once again.
The cleverness of this arrangement is that your app, when the user returns to it after having left it to use some other app for a while, is found in thevery same stateas when the user left it. The app was not terminated; it simply stopped and froze, and waited in suspended animation. Returning to your app no longer means that your app islaunched, but merely that it isresumed.
All of this is not to say, however, that your appcan’tbe terminated. It can be — though not by the user pressing the Home button. The most common scenario is that the system quietly kills your app while it is suspended. This undermines the app’s ability to resume;
when the user returns to your app, itwillhave to launch from scratch, just as in the pre–iOS 4 days. The death of your app is rather like that of the scientists killed by HAL 9000 in2001: A Space Odyssey— they went to sleep expecting to wake up later, but instead their life-support systems were turned off while they slept. The iOS system’s reasons for killing your app are not quite as paranoid as HAL’s, but they do have a certain Darwinian ruthlessness: your app, while suspended, continues to occupy a chunk of the device’s memory, and the system needs to reclaim that memory so someotherapp can use it. It is also possible, of course, that the user will switch off the device while your app is asleep.