代理可以处理的事件包括:
1、监听应用程序的生命周期事件(如程序启动和关闭)
2、系统事件(如来电)
3、内存警告
UIApplicationの常用代理方法(应用程序的生命周期方法)
//Tells the delegate that the launch process is almost done and the app is almost ready to run.应用程序启动完成时调用
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { returnYES; }
//Tells the delegate that the app is about to become inactive.当应用程序即将失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication*)application { }
//Tells the delegate that the app is now in the background.当应用程序完全进入后台的时候调用
- (void)applicationDidEnterBackground:(UIApplication*)application { }
//Tells the delegate that the app is about to enter the foreground.当应用程序即将进入前台的时候调用
- (void)applicationWillEnterForeground:(UIApplication*)application { }
//Tells the delegate that the app has become active.当应用程序完全获得焦点的时候调用
- (void)applicationDidBecomeActive:(UIApplication*)application { }
//Tells the delegate when the app is about to terminate.当应用程序即将关闭的时候调用
- (void)applicationWillTerminate:(UIApplication*)application { }
//Tells the delegate when the app receives a memory warning from the system.接受到内存警告的时候调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application{ }
应用程序的启动流程
程序的入口main函数。
UIApplicationMain(int argc, char* argv[], NSString *__nullable principalClassName, NSString *__nullable delegateClassName);
第三个参数:UIApplication类名或者子类的名称。nil == @"UIApplication" 。
第四个参数:UIApplication代理的名称。@"AppDelegate"。
UIApplicationMain (argc, argv, nil, NSStringFromClass([AppDelegateclass]));
所以系统的写法等价于:
UIApplicationMain (argc, argv, @"UIApplication", @"AppDelegate");
UIApplicationMain底层实现:
1、根据principalClassName传递的类名创建UIApplication对象。
2、创建UIApplication代理对象,给UIApplication对象设置代理。
3、开启主运行事件循环,处理事件。
4、加载info.plist,判断是否指定了main nib(story board),如果指定了就会去加载。