APP 的生命周期

本人有若干成套学习视频, 可试看! 可试看! 可试看, 重要的事情说三遍 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

先上一张图, 让大家有一个直观的印象, 这图是我用 keynote 画的, 第一次使用, 大家多多包涵
APP 生命周期示意图
下面我就来一一解释下 APP 的生命周期
  • 当我们打开 APP 时, 首先会执行 main函数, 但是与 OC 不同的是, swift 并不能找到 main.swift 这个文件, 这是为什么呢?
swift 中main函数的执行

当我们在19行打上一个断点时, 可以发现, swift 当然也执行了 main 函数,只不过,苹果没有给我们提供 main 函数的接口, 大概苹果认为这个反正不常用, 就干脆不单独设置了吧, 于是就用了@ UIApplicationMain 这么一个关键字合并了 APP 入口.

苹果给出的官方解释如下:

For iOS apps the default for new iOS project templates is to add @UIApplicationMain to a regular Swift file. This causes the compiler to synthesize a mainentry point for your iOS app, and eliminates the need for a “main.swift” file.

就是说, iOS APP 的默认的项目模板中添加了@UIApplicationMain关键字, 这使得编译器合成了 iOS APP 的 main 入口, 所有就不需要有 main.swift 的存在.

那么在 main函数中, 系统帮我们做了什么事情呢?

系统主要帮我们
1.初始化 UIApplication 对象;
2.设置 applicationDelegate;
3.开启运行循环,监听系统事件.

这个运行循环有什么特点呢? 它遵循 FIFO 的原则,即 First In First Out(先进先出)的原则, 先监测到的事件先处理, 当没有监测到事件的时候处于休眠状态

当 APP 对象监测到系统事件的时候, 无法处理事件, 这个时候就要用到代理, AppDelegate 这个类遵守了 UIApplicationDelegate 这个代理协议, 并实现了处理 APP 生命周期事件的方法, 这些方法就是第一幅图中提到的那些方法:

  • 程序加载完毕
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        print("applicationDidFinishLaunchingWithOptions")
        return true
    }

应用启动并进行初始化时会调用该方法并发出通知: UIApplicationDidFinishLaunchingNotification.这个阶段会实例化根视图控制器. 也可以写一些希望在程序加载完毕之后进行的操作.

  • 程序获取焦点
    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        print("applicationDidBecomeActive")
    }

应用已经进入前台, 并处于活动状态时调用该方法并发出通知 UIApplicationDidBecomeActiveNotification. 这个阶段可以重启任意在程序处于非激活状态下的暂停的任务(或者是尚未开始的任务), 如果应用程序之前处于后台, 可以进行刷新 UI 的操作(非必须)

  • 程序将要失去焦点(即将被挂起)
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
        print("applicationWillResignActive")
    }

应用即将失去焦点(即将被挂起), 并发出 UIApplicationWillResignActiveNotification 通知, 这个方法是在激活到失活状态时调用, 比如收到短信或者有电话呼入, 或者当用户退出运用程序到后台的转场时调用. 在这个方法里, 我们需要写一些暂停当前任务, 暂停计时器, 干掉图形渲染回调, 终止游戏等代码

  • 程序已经进入后台
    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        print("applicationDidEnterBackground")
    }

应用程序已经进入后台, 并发出 UIApplicationDidEnterBackgroundNotification 通知, 使用这个方法释放共享资源, 保存用户数据, 干掉计时器, 尽可能地储存当前的应用状态信息.

  • 程序即将进入前台
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        print("applicationWillEnterForeground")
    }

应用即将进入前台, 但是还没有处于活动状态时调用该方法,并发出 UIApplicationWillEnterForeground通知, 就是这个时候应用还无法响应用户的点击的交互事件, 这个阶段可以恢复用户的数据.

  • 程序即将终止
    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        print("applicationWillTerminate")
    }

应用即将被终止时调用, 并发出通知UIApplicationWillTerminate, 这个阶段可以保存用户数据

  • 应用程序接收到内存警告

严格说来, 这并不是 APP 生命周期的一部分
当应用程序收到系统发出的内存警告时调用, 主要在这里写一些清除缓存的代码.

以上就是 APP 的生命周期的大致内容. 开头的图再放一遍:

APP 生命周期示意图

PS. 本人有若干成套学习视频, 包含Java, 数据结构与算法, iOS, 安卓, python, flutter等等, 如有需要, 联系微信tsaievan.

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

推荐阅读更多精彩内容