代理可以處理的事件包括:
1峰搪、監(jiān)聽?wèi)?yīng)用程序的生命周期事件(如程序啟動(dòng)和關(guān)閉)
2望几、系統(tǒng)事件(如來電)
3、內(nèi)存警告
UIApplicationの常用代理方法(應(yīng)用程序的生命周期方法)
//Tells the delegate that the launch process is almost done and the app is almost ready to run.應(yīng)用程序啟動(dòng)完成時(shí)調(diào)用
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { ?returnYES; ?}
//Tells the delegate that the app is about to become inactive.當(dāng)應(yīng)用程序即將失去焦點(diǎn)的時(shí)候調(diào)用
- (void)applicationWillResignActive:(UIApplication*)application { ?}
//Tells the delegate that the app is now in the background.當(dāng)應(yīng)用程序完全進(jìn)入后臺(tái)的時(shí)候調(diào)用
- (void)applicationDidEnterBackground:(UIApplication*)application { ?}
//Tells the delegate that the app is about to enter the foreground.當(dāng)應(yīng)用程序即將進(jìn)入前臺(tái)的時(shí)候調(diào)用
- (void)applicationWillEnterForeground:(UIApplication*)application { ?}
//Tells the delegate that the app has become active.當(dāng)應(yīng)用程序完全獲得焦點(diǎn)的時(shí)候調(diào)用
- (void)applicationDidBecomeActive:(UIApplication*)application { ?}
//Tells the delegate when the app is about to terminate.當(dāng)應(yīng)用程序即將關(guān)閉的時(shí)候調(diào)用
- (void)applicationWillTerminate:(UIApplication*)application { ?}
//Tells the delegate when the app receives a memory warning from the system.接受到內(nèi)存警告的時(shí)候調(diào)用
- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application{ ?}
應(yīng)用程序的啟動(dòng)流程
程序的入口main函數(shù)。
UIApplicationMain(int argc, char* argv[], NSString *__nullable principalClassName, NSString *__nullable delegateClassName);
第三個(gè)參數(shù):UIApplication類名或者子類的名稱囊咏。nil == @"UIApplication" 。
第四個(gè)參數(shù):UIApplication代理的名稱塔橡。@"AppDelegate"梅割。
UIApplicationMain (argc, ?argv, nil, NSStringFromClass([AppDelegateclass]));
所以系統(tǒng)的寫法等價(jià)于:
UIApplicationMain (argc, ?argv, @"UIApplication", @"AppDelegate");
UIApplicationMain底層實(shí)現(xiàn):
1、根據(jù)principalClassName傳遞的類名創(chuàng)建UIApplication對(duì)象葛家。
2户辞、創(chuàng)建UIApplication代理對(duì)象,給UIApplication對(duì)象設(shè)置代理癞谒。
3底燎、開啟主運(yùn)行事件循環(huán),處理事件弹砚。
4双仍、加載info.plist,判斷是否指定了main nib(story board)桌吃,如果指定了就會(huì)去加載朱沃。