IOS应用的启动流程

程序入口为main函数,如下以原始未改动的main函数为例app

int main(int argc, char * argv[]) {
    @autoreleasepool {
         NSLog(@"%s",__func__);
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
       
    }
}
  1. main函数中会调用 UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);第3、四个参数表明主要类和代理类,若主要类为nil,则默认为UIApplication,若是代理类为nil,则从Main nib文件中加载代理对象。UIApplication对象会监控应用程序的生命周期,并由代理对象处理监听到的事件。函数

  2. 应用会开启主运行循环,进行事件的处理(首先会启动完毕后调用代理对象的如下方法)ui

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

        在这个方法中会实例化一个UIWindow对象设置其属性,如frame,设为代理对象的主窗口,还有就是设置UIWindow的根视图控制器,显示主窗口,程序会根据根视图控制器的具体状况加载对应的View到主窗口上。this

    3.UIApplication代理对象中,和生命周期有关的部分方法以下
spa

    a.当应用程序进入非活动状态时调用,在此期间不接收消息或事件,好比来电
代理

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

    b.当程序被推送到后台时调用,若要设置后台继续运行,可在此方法中设置
rest

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

    c.当程序将从后台转为前台运行时调用。
code

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

    d.当程序进入活动状态时执行,可接收消息或事件
orm

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

    e.当程序退出时调用,一般是保存数据,和清理工做
对象

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
相关文章
相关标签/搜索