開發(fā)應(yīng)用程序都要了解其生命周期,開始接觸android時(shí)也是從應(yīng)用程序生命周期開始的,android的應(yīng)用程序生命周期更多是其組件的生命周期,例如Activity、Service。今天我們接觸一下iOS應(yīng)用程序的生命周期,
iOS的入口在main.m文件:
int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
UIApplicationMain函數(shù),前兩個(gè)和main函數(shù)一樣,重點(diǎn)是后兩個(gè),官方說明是這樣的:
// 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 *principalClassName, NSString *delegateClassName);
后兩個(gè)參數(shù)分別表示程序的主要類(principal class)和代理類(delegate class)。如果主要類(principal class)為nil,將從Info.plist中獲取,如果Info.plist中不存在對(duì)應(yīng)的key,則默認(rèn)為UIApplication;如果代理類(delegate class)將在新建工程時(shí)創(chuàng)建。
根據(jù)UIApplicationMain函數(shù),程序?qū)⑦M(jìn)入AppDelegate.m,這個(gè)文件是xcode新建工程時(shí)自動(dòng)生成的。下面看一下AppDelegate.m文件,這個(gè)關(guān)乎著應(yīng)用程序的生命周期。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; NSLog(@"iOS_didFinishLaunchingWithOptions"); 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. NSLog(@"iOS_applicationWillResignActive"); } - (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. NSLog(@"iOS_applicationDidEnterBackground"); } - (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. NSLog(@"iOS_applicationWillEnterForeground"); } - (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. NSLog(@"iOS_applicationDidBecomeActive"); } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"iOS_applicationWillTerminate"); }
2、applicationWillResignActive:在應(yīng)用程序?qū)⒁苫顒?dòng)狀態(tài)切換到非活動(dòng)狀態(tài)時(shí)候,要執(zhí)行的委托調(diào)用,如 按下 home 按鈕,返回主屏幕,或全屏之間切換應(yīng)用程序等。
3、applicationDidEnterBackground:在應(yīng)用程序已進(jìn)入后臺(tái)程序時(shí),要執(zhí)行的委托調(diào)用。
4、applicationWillEnterForeground:在應(yīng)用程序?qū)⒁M(jìn)入前臺(tái)時(shí)(被激活),要執(zhí)行的委托調(diào)用,剛好與applicationWillResignActive 方法相對(duì)應(yīng)。
5、applicationDidBecomeActive:在應(yīng)用程序已被激活后,要執(zhí)行的委托調(diào)用,剛好與applicationDidEnterBackground 方法相對(duì)應(yīng)。
6、applicationWillTerminate:在應(yīng)用程序要完全推出的時(shí)候,要執(zhí)行的委托調(diào)用,這個(gè)需要要設(shè)置UIApplicationExitsOnSuspend的鍵值。
初次啟動(dòng):
2013-05-24 20:20:31.550 LifeIOS[451:c07] iOS_didFinishLaunchingWithOptions
2013-05-24 20:20:31.551 LifeIOS[451:c07] iOS_applicationDidBecomeActive
按下home鍵:2013-05-24 20:22:17.349 LifeIOS[451:c07] iOS_applicationWillResignActive
2013-05-24 20:22:17.350 LifeIOS[451:c07] iOS_applicationDidEnterBackground
點(diǎn)擊程序圖標(biāo)進(jìn)入:2013-05-24 20:22:56.913 LifeIOS[451:c07] iOS_applicationWillEnterForeground
2013-05-24 20:22:56.914 LifeIOS[451:c07] iOS_applicationDidBecomeActive
程序中沒有設(shè)置UIApplicationExitsOnSuspend的值,程序不會(huì)徹底退出。
看上面iOS的生命周期,是不是和Android的Activity生周期也相似,所以說程序是相通的,對(duì)比著學(xué)習(xí)也是收獲大的。
/**另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站欄目:iOS學(xué)習(xí)——iOS應(yīng)用程序生命周期(四)-創(chuàng)新互聯(lián)
分享路徑:http://www.aaarwkj.com/article30/coihso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營銷推廣、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容