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程序的啟動過程流程圖

流程圖