Programming iOS 9 - APPENDIX A Application Lifetime Events

从附录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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容