一、main函數(shù)
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
iOS程序首先由main函數(shù)執(zhí)行泛烙,由如上代碼可以看到main函數(shù)執(zhí)行后程序進入UIApplicationMain函數(shù)。
二、UIApplicationMain函數(shù)
// 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函數(shù)原型可以看到宦言,函數(shù)的第三個參數(shù)princlepalClass是應(yīng)用程序類UIApplication,第四個參數(shù)是程序代理類AppDelegate商模。在UIApplicationMain中主要做三件事:
- 創(chuàng)建UIApplication對象
- 創(chuàng)建AppDelegate對象
- 開啟事件循環(huán)
三奠旺、UIApplication
UIApplicationMain執(zhí)行后先初始化UIApplication對象。UIApplication對象是應(yīng)用程序的核心施流,每個App只有一個UIApplication實例(通過[UIApplication shareApplication]獲认炀巍)。UIApplication的最主要作用作用是作為一個應(yīng)用程序的核心瞪醋,程序事件首先到達UIApplication中忿晕,然后再由UIApplication進行分發(fā)。另外UIApplication還提供openURL打開其他應(yīng)用程序银受、注冊遠程践盼、本地通知等功能鸦采。
四、AppDelegate
UIApplication初始化后進入Appdelegate的didFinishLaunchingWithOptions函數(shù)中咕幻,此時應(yīng)用程序界面真正開始創(chuàng)建渔伯。Appdelegate顧名思義是程序代理類,它的作用是處理一些App生命周期中出現(xiàn)的重要事件肄程,如程序狀態(tài)發(fā)生改變(前臺到后臺等)锣吼,接收到遠程、本地通知绷耍,初始化程序視圖等吐限。簡言之,UIApplication是接收事件褂始,而事件處理則交由程序代理類AppDelegate執(zhí)行诸典。
當程序運行到Appdelegate的didFinishLaunchingWithOptions時會創(chuàng)建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:.
}