UIApplication和delegate
- 所有的移動操作系統(tǒng)都有個致命的缺點:app很容易受到打擾卒稳。比如一個來電或者是鎖屏會導致app進入后臺甚至終止弧轧。
- 還有很多其他類似的情況都會導致app受到干擾鬼廓,app受到干擾時趁怔,會產(chǎn)生一系列系統(tǒng)事件郑兴,這時UIApplication會通知他的delegate對象,讓delegate來處理系統(tǒng)事件
delegate可處理的事件包括:
- 應用程序的生命周期事件(如程序的啟動和關閉)
- 系統(tǒng)事件(如來電)
- 內(nèi)存警告
UIApplication和delegate
delegate方法講解:
//應用程序啟動完成后就會調(diào)用AppDelegate方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
//當應用程序失去焦點的時候調(diào)用,只有當應用程序完全獲取焦點的時候才能夠與用戶交互,所謂的獲取焦點我的理解為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.
}
//當應用程序進入后臺時候調(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.
}
//當應用程序即將進入前臺的時候調(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.
}
//當應用程序獲得焦點的時候調(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.
}
//當應用程序關閉的時候調(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.點擊應用重新進入
application進入前臺然后獲得焦點
程序進入前臺
4.雙擊home鍵關閉程序
程序關閉
關閉程序
由控制臺打印結(jié)果可知按照上面的操作,先后順序為:
程序加載完畢-->程序獲得焦點-->程序失去焦點-->程序進入后臺-->程序進入前臺-->程序獲得焦點-->程序關閉
程序的啟動流程
講解
演示main以及控制臺打印結(jié)果
查看官方文檔炭菌,了解參數(shù)的介紹
第三個參數(shù):UIApplication類名或者子類的名稱跃须,如果傳nil的話默認為@“UIApplication”
第四個參數(shù):UIApplication的代理的類型名稱,蘋果使用NSStringFromClass([AppDelegate class])方法娃兽,獲得對應類名,使用該方法除了可以避免輸入錯誤的同時還具有提示功能尽楔。
官方文檔
UIApplication底層實現(xiàn)原理:
1.根據(jù)principalClassName(第三個參數(shù))傳遞的類名創(chuàng)建一個UIApplication對象
2.創(chuàng)建UIApplication代理對象投储,給UIApplication對象設置代理
3.開啟主運行事件循環(huán)第练,處理事件,保持程序一直運行(后期runloop講解)
4.加載Info.plist玛荞,判斷application的Info.pist文件是否指定main娇掏,如果指定main就會記載nib文件
iOS程序的啟動過程流程圖
流程圖