夯實基礎(chǔ) 砥礪前行 --- UIApplication(中)

UIApplication和delegate

  • 所有的移動操作系統(tǒng)都有個致命的缺點:app很容易受到打擾。比如一個來電或者是鎖屏?xí)?dǎo)致app進入后臺甚至終止。
  • 還有很多其他類似的情況都會導(dǎo)致app受到干擾,app受到干擾時,會產(chǎn)生一系列系統(tǒng)事件,這時UIApplication會通知他的delegate對象,讓delegate來處理系統(tǒng)事件

delegate可處理的事件包括:

  • 應(yīng)用程序的生命周期事件(如程序的啟動和關(guān)閉)
  • 系統(tǒng)事件(如來電)
  • 內(nèi)存警告
UIApplication和delegate

delegate方法講解:

//應(yīng)用程序啟動完成后就會調(diào)用AppDelegate方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}
//當應(yīng)用程序失去焦點的時候調(diào)用,只有當應(yīng)用程序完全獲取焦點的時候才能夠與用戶交互,所謂的獲取焦點我的理解為app啟動布滿屏幕。
- (void)applicationWillResignActive:(UIApplication *)application {
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
//當應(yīng)用程序進入后臺時候調(diào)用
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 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.
}
//當應(yīng)用程序即將進入前臺的時候調(diào)用
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
//當應(yīng)用程序獲得焦點的時候調(diào)用
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 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.
}
//當應(yīng)用程序關(guān)閉的時候調(diào)用
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

各方法的調(diào)用時機的演示以及講解
1.程序一啟動未做任何操作
程序加載完畢,并獲得焦點


程序啟動

2.點擊home鍵進入手機主界面
application先失去焦點然后 進入后臺


程序進入后臺

3.點擊應(yīng)用重新進入
application進入前臺然后獲得焦點


程序進入前臺

4.雙擊home鍵關(guān)閉程序
程序關(guān)閉
關(guān)閉程序

由控制臺打印結(jié)果可知按照上面的操作,先后順序為:
程序加載完畢-->程序獲得焦點-->程序失去焦點-->程序進入后臺-->程序進入前臺-->程序獲得焦點-->程序關(guān)閉

程序的啟動流程

講解

演示main以及控制臺打印結(jié)果

查看官方文檔,了解參數(shù)的介紹
第三個參數(shù):UIApplication類名或者子類的名稱,如果傳nil的話默認為@“UIApplication”
第四個參數(shù):UIApplication的代理的類型名稱,蘋果使用NSStringFromClass([AppDelegate class])方法,獲得對應(yīng)類名,使用該方法除了可以避免輸入錯誤的同時還具有提示功能。


官方文檔

UIApplication底層實現(xiàn)原理:

1.根據(jù)principalClassName(第三個參數(shù))傳遞的類名創(chuàng)建一個UIApplication對象
2.創(chuàng)建UIApplication代理對象,給UIApplication對象設(shè)置代理
3.開啟主運行事件循環(huán),處理事件,保持程序一直運行(后期runloop講解)
4.加載Info.plist,判斷application的Info.pist文件是否指定main,如果指定main就會記載nib文件

iOS程序的啟動過程流程圖

流程圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,536評論 19 139
  • 程序啟動原理 掌握 Info.plist和pch文件的作用 UIApplication的常見使用 AppDeleg...
    JonesCxy閱讀 899評論 0 1
  • 父類實現(xiàn)深拷貝時,子類如何實現(xiàn)深度拷貝。父類沒有實現(xiàn)深拷貝時,子類如何實現(xiàn)深度拷貝。? 深拷貝同淺拷貝的區(qū)別:淺拷...
    JonesCxy閱讀 1,196評論 1 7
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,922評論 25 709
  • 在周末的午后,如果不出門,總覺得有些孤單的可怕,在諾大的城市,卻在狹小的房間一個人悄無聲息的過著。早上一覺睡到十點...
    waitine閱讀 582評論 9 2

友情鏈接更多精彩內(nèi)容