iOS程序的啟動執(zhí)行順序

iOS程序的啟動執(zhí)行順序

圖來自林大鵬天地

iOS程序的啟動執(zhí)行順序

1 程序的入口

進入main函數(shù), 設(shè)置AppDelegate稱為函數(shù)的代理

2 ?程序完成加載

-[AppDelegate application:didFinishLaunchingWithOptions:]

3 創(chuàng)建window窗口

4 程序被激活

-[AppDelegate applicationDidBecomeActive:]

5 當點擊command+H時

程序取消激活狀態(tài)

-[AppDelegate applicationWillResignActive:]

程序進入后臺

-[AppDelegate applicationDidEnterBackground:]

6 點擊進入工程

程序進入前臺

-[AppDelegate applicationWillEnterForeground:]

程序被激活

-[AppDelegate applicationDidBecomeActive:]


對于applicationWillResignActive(非活動)與applicationDidEnterBackground(后臺)這兩個有點分不清。

applicationWillResignActive(非活動):比如當有電話進來或短信進來,在或者鎖屏等,這時你的應(yīng)用程序掛起進入非活動狀態(tài),也就是你的手機其實界面還是顯示著你當前的App窗口,只不過被別的任務(wù)強制占用了,或者后臺狀態(tài)(因為要先進入非活動狀態(tài),然后進入后臺)。

applicationDidEnterBackground(后臺):指當前窗口不是你的App,大多數(shù)程序進入這個后臺后會在在這個狀態(tài)上停留一會,時間到之后會進入掛起狀態(tài)(Suspended)。

如果你程序特殊處理后可以長期處于后臺狀態(tài)即在后臺狀態(tài)也可以運行。Suspended(掛起):程序在后臺不能執(zhí)行代碼。系統(tǒng)會自動把程序變成這個狀態(tài)而且不會發(fā)出通知。當掛起時,程序還是停留在內(nèi)存中的,當系統(tǒng)內(nèi)存低時,系統(tǒng)就把掛起的程序清除掉,為前臺程序提供更多的內(nèi)存。

用回這個圖


入口的函數(shù)是main函數(shù)

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

@autoreleasepool {

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

}

}

argc和argv是為了與C語言保持一致,后面兩個參數(shù)為principalClassName(主要類名)和delegateClassName(委托類名)。如果principalClassName是nil,那么它的值將從Info.plist中獲取,如果Info.plist中沒有,則默認為UIApplication。

delegateClass將在工程新建時實例化一個對象

NSStringFromClass([AppDelegate class])

//相當于@"AppDelegate"



而在AppDelegate類實現(xiàn)文件

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

{

// Override point for customization after application launch.

NSLog(@"didFinishLaunchingWithOptions");

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

/*

當應(yīng)用程序從活動狀態(tài)(active)變到非活動狀態(tài)(inactive時被觸發(fā)調(diào)用,

這可能發(fā)生在一些臨時中斷下(例如:來電話、來短信)又或者程序退出時,他會先過渡到后臺然后terminate

使用這方法去暫停正在進行的任務(wù),禁用計時器,節(jié)流OpenGL ES 幀率。在游戲中應(yīng)該在這個方法里面暫停游戲。

*/

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

{

/*

使用這種方法來釋放共享資源,保存用戶數(shù)據(jù),無效計時器,存儲足夠多的應(yīng)用程序狀態(tài)信息來恢復(fù)您的應(yīng)用程序的當前狀態(tài),以防它終止丟失數(shù)據(jù)。

如果你的程序支持后臺運行,那么當用戶退出時不會調(diào)用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

{

/*

先從后臺切換到非活動狀態(tài),然后進入活動狀態(tài)。

*/

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

{

/*

重啟所有的任務(wù),不管是從非活動狀態(tài)還是剛啟動程序,還是后臺狀態(tài)。

*/

// 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");

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容