(一)iOS程序运行原理

一、main函数

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

iOS程序首先由main函数执行,由如上代码可以看到main函数执行后程序进入UIApplicationMain函数。

二、UIApplicationMain函数

// If nil is specified for principalClassName, the value for NSPrincipalClass from the Info.plist is used. If there is no
// NSPrincipalClass key specified, the UIApplication class is used. The delegate class will be instantiated using init.
UIKIT_EXTERN int UIApplicationMain(int argc, char *argv[], NSString * __nullable principalClassName, NSString * __nullable delegateClassName);

由如上UIApplicationMain函数原型可以看到,函数的第三个参数princlepalClass是应用程序类UIApplication,第四个参数是程序代理类AppDelegate。在UIApplicationMain中主要做三件事:

  • 创建UIApplication对象
  • 创建AppDelegate对象
  • 开启事件循环

三、UIApplication

UIApplicationMain执行后先初始化UIApplication对象。UIApplication对象是应用程序的核心,每个App只有一个UIApplication实例(通过[UIApplication shareApplication]获取)。UIApplication的最主要作用作用是作为一个应用程序的核心,程序事件首先到达UIApplication中,然后再由UIApplication进行分发。另外UIApplication还提供openURL打开其他应用程序、注册远程、本地通知等功能。

四、AppDelegate

UIApplication初始化后进入Appdelegate的didFinishLaunchingWithOptions函数中,此时应用程序界面真正开始创建。Appdelegate顾名思义是程序代理类,它的作用是处理一些App生命周期中出现的重要事件,如程序状态发生改变(前台到后台等),接收到远程、本地通知,初始化程序视图等。简言之,UIApplication是接收事件,而事件处理则交由程序代理类AppDelegate执行。

当程序运行到Appdelegate的didFinishLaunchingWithOptions时会创建UIWindow,我们把自定义ViewController加入UIWindow中,此时程序视图真正开始建立。
- (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 throttle down OpenGL ES frame rates. 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 inactive 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:.
}

推荐阅读更多精彩内容