iOS程序的启动执行顺序

iOS程序的启动执行顺序

iOS程序的启动执行顺序_第1张图片
图来自林大鹏天地

iOS程序的启动执行顺序

1 程序的入口

进入main函数, 设置AppDelegate称为函数的代理

2  程序完成加载

-[AppDelegate application:didFinishLaunchingWithOptions:]

3 创建window窗口

4 程序被激活

-[AppDelegate applicationDidBecomeActive:]

5 当点击command+H时

程序取消激活状态

-[AppDelegate applicationWillResignActive:]

程序进入后台

-[AppDelegate applicationDidEnterBackground:]

6 点击进入工程

程序进入前台

-[AppDelegate applicationWillEnterForeground:]

程序被激活

-[AppDelegate applicationDidBecomeActive:]


对于applicationWillResignActive(非活动)与applicationDidEnterBackground(后台)这两个有点分不清。

applicationWillResignActive(非活动):比如当有电话进来或短信进来,在或者锁屏等,这时你的应用程序挂起进入非活动状态,也就是你的手机其实界面还是显示着你当前的App窗口,只不过被别的任务强制占用了,或者后台状态(因为要先进入非活动状态,然后进入后台)。

applicationDidEnterBackground(后台):指当前窗口不是你的App,大多数程序进入这个后台后会在在这个状态上停留一会,时间到之后会进入挂起状态(Suspended)。

如果你程序特殊处理后可以长期处于后台状态即在后台状态也可以运行。Suspended(挂起):程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。

用回这个图

iOS程序的启动执行顺序_第2张图片

入口的函数是main函数

int main(int argc, char * argv[]){

@autoreleasepool {

return UIApplicationMain(argc, argv, nil, NSStringFromClass([XYZAppDelegate class]));

}

}

argc和argv是为了与C语言保持一致,后面两个参数为principalClassName(主要类名)和delegateClassName(委托类名)。如果principalClassName是nil,那么它的值将从Info.plist中获取,如果Info.plist中没有,则默认为UIApplication。

delegateClass将在工程新建时实例化一个对象

NSStringFromClass([AppDelegate class])

//相当于@"AppDelegate"



而在AppDelegate类实现文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

// Override point for customization after application launch.

NSLog(@"didFinishLaunchingWithOptions");

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

/*

当应用程序从活动状态(active)变到非活动状态(inactive时被触发调用,

这可能发生在一些临时中断下(例如:来电话、来短信)又或者程序退出时,他会先过渡到后台然后terminate

使用这方法去暂停正在进行的任务,禁用计时器,节流OpenGL ES 帧率。在游戏中应该在这个方法里面暂停游戏。

*/

// 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.

NSLog(@"WillResignActive");

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

/*

使用这种方法来释放共享资源,保存用户数据,无效计时器,存储足够多的应用程序状态信息来恢复您的应用程序的当前状态,以防它终止丢失数据。

如果你的程序支持后台运行,那么当用户退出时不会调用applicationWillTerminate。

*/

// 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.

NSLog(@"DidEnterBackground");

}

- (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.

NSLog(@"WillEnterForeground");

}

- (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.

NSLog(@"DidBecomeActive");

}

- (void)applicationWillTerminate:(UIApplication *)application

{

/*

终止,game over

*/

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

NSLog(@"WillTerminate");

}

你可能感兴趣的:(iOS程序的启动执行顺序)