從附錄A開始吧. 因?yàn)槔斫鈶?yīng)用的生命周期是理解開發(fā)的基礎(chǔ)。
#應(yīng)用生命周期事件
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.
你的應(yīng)用唯一的“Apllication"對象(一個(gè)UIApplication實(shí)例,或者在很少情況下之景,一個(gè)UIApplication子類的實(shí)例)由UIApplicationMain為你創(chuàng)建勤婚。同時(shí)它也創(chuàng)建了“Application”對象的delegate對象元镀。在Xcode項(xiàng)目模版里口锭,這個(gè)delegate對象是AppDelegate類的實(shí)例类嗤。應(yīng)用程序通過方法調(diào)用delegate對象來報(bào)告生命周期中的一些事件祈餐;其它實(shí)例也可以注冊來當(dāng)作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.
這些事件非常重要也非嘲饣梗基礎(chǔ)才避,它們告知你的應(yīng)用當(dāng)前在整個(gè)應(yīng)用生命周期中所處的階段從而給你用代碼來響應(yīng)這些階段的機(jī)會(huì)。這個(gè)附錄會(huì)借一些這些事件出現(xiàn)的典型場景專門討論這些事件氨距。
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.
應(yīng)用程序的狀態(tài)集
在在早期的iOS4之前的iOS版本中桑逝,app的生命周期超級簡單,它不是在運(yùn)行衔蹲,就是沒運(yùn)行肢娘。用戶在屏幕上點(diǎn)應(yīng)用的圖標(biāo),應(yīng)用就啟動(dòng)然后開始運(yùn)行舆驶。用戶使用一段時(shí)間之后橱健,用戶按Home鍵,然后你的應(yīng)用就終止了沙廉,也就是不在運(yùn)行狀態(tài)了拘荡。用戶退出了你的應(yīng)用。啟動(dòng)撬陵,運(yùn)行珊皿,退出:這就是那時(shí)候應(yīng)用的整個(gè)生命周期。如果用戶決定再次使用你的應(yīng)用巨税,整個(gè)循環(huán)又一次開始蟋定。
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設(shè)備的處理器比較慢驶兜。總的來說远寸,系統(tǒng)的資源只夠跑一個(gè)程序的抄淑。(一個(gè)應(yīng)用在運(yùn)行時(shí)占用的不僅僅是屏幕,還包括大量其它資源)驰后。
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 nai?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.