Programming iOS 9 - APPENDIX A Application Lifetime Events

從附錄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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肆资,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子灶芝,更是在濱河造成了極大的恐慌郑原,老刑警劉巖唉韭,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異犯犁,居然都是意外死亡纽哥,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門栖秕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人晓避,你說我怎么就攤上這事簇捍。” “怎么了俏拱?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵暑塑,是天一觀的道長。 經(jīng)常有香客問我锅必,道長事格,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任搞隐,我火速辦了婚禮驹愚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘劣纲。我一直安慰自己逢捺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布癞季。 她就那樣靜靜地躺著劫瞳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪绷柒。 梳的紋絲不亂的頭發(fā)上志于,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天,我揣著相機(jī)與錄音废睦,去河邊找鬼伺绽。 笑死,一個(gè)胖子當(dāng)著我的面吹牛郊楣,可吹牛的內(nèi)容都是我干的憔恳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼净蚤,長吁一口氣:“原來是場噩夢啊……” “哼钥组!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起今瀑,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤程梦,失蹤者是張志新(化名)和其女友劉穎点把,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屿附,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡郎逃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了挺份。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片褒翰。...
    茶點(diǎn)故事閱讀 40,021評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖匀泊,靈堂內(nèi)的尸體忽然破棺而出优训,到底是詐尸還是另有隱情,我是刑警寧澤各聘,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布揣非,位于F島的核電站,受9級特大地震影響躲因,放射性物質(zhì)發(fā)生泄漏早敬。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一大脉、第九天 我趴在偏房一處隱蔽的房頂上張望搞监。 院中可真熱鬧,春花似錦箱靴、人聲如沸腺逛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽棍矛。三九已至,卻和暖如春抛杨,著一層夾襖步出監(jiān)牢的瞬間够委,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工怖现, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茁帽,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓屈嗤,卻偏偏與公主長得像潘拨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子饶号,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容