下面我们来介绍点击应用图标后,应用程序的启动。我们都知道,C语言是从main函数开始执行代码的。OC作为C语言的超集,当然也不例外。点击图标,开始执行main函数。iOS项目中的main函数是在创建项目的时候就已经写好了的
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
我们先来看一下 UIApplicationMain的这几个参数
- argc 表示参数个数
- argv 表示函数参数的数组
- 是UIApplication类名或者是其子类名,如果是nil,则就默认使用 UIApplication类名
4.是协议UIApplicationDelegate的实例化对象名,如果是nil,则从main nib文件中加载委托对象。这个对象就是UIApplication对象监听到系统变化的时候通知其执行的相应方法
此函数会创建一个UIApplication对象,并根据第四个参数创建一个delegate对象,然后将此delegate对象赋值给UIApplication对象的delegate属性。
接着会建立应用程序的Main Runloop循环,调用UIApplicationDelegate中的函数进行事件处理
UIApplication
UIApplication代表一个应用程序,每一个应用程序都有一个UIApplication全局对象,而且这个对象是单例的,我们在程序中可以通过[UIApplication sharedApplication]获得这个对象,进行一些应用级的操作。UIApplication是应用程序的开始,它维护了一个在本应用程序中打开的Window列表,负责初始化显示UIWindow,并负责加载应用程序的第一个UIView到UIWindow窗口中,在随后的操作中,我们也可以更换UIWindow窗口的显示内容。此外,UIApplication还被赋予一个代理对象,在实际编程中,我们一般并不直接与UIApplication打交道,而是和其代理对象UIApplicationDelegate打交道,UIApplication负责监听接收事件,而由UIApplicationDelegate决定应用程序如何去响应这些事件(生命周期:程序启动和关闭,系统事件:来电、记事项警告)等等
UIApplicationDelegate
所有的移动操作系统都有个致命的缺点:app很容易受到打扰.比如一个来电或者锁屏会导致app进入后台甚至被终止;还有很多其他类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate来处理这些系统事件.由图二我们可以看到代理类可以处理许多事件
1.程序加载完毕
2.程序获取焦点
3.程序进入后台
4.程序失去焦点
5.程序从后台回到前台
6.内存不足警告
7.程序即将退出
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (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.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (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.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
启动流程
1.main函数
2.执行UIApplicationMain函数
1.创建UIApplication对象
2.创建UIApplicationDelegate对象并复制
3.读取配置文件info.plist,设置程序启动的一些属性
4.创建应用程序的Main Runloop循环
3.UIApplicationDelegate对象开始处理监听到的事件
1.程序启动成功之后,首先调用<code>application:didFinishLaunchingWithOptions:</code>方法,
如果info.plist文件中配置了启动storyboard文件名,则加载storyboard文件。
如果没有配置,则根据代码来创建UIWindow--->UIWindow的rootViewController
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//所要显示的视图控件
LJTViewControler *LJTVC = [[LJTViewController alloc] init];
self.window.rootViewController=LJTVC;
[self.window makeKeyAndVisible];
return YES;
}