一、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:.
}