溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS應(yīng)用程序生命周期的示例分析

發(fā)布時(shí)間:2021-08-04 13:47:08 來源:億速云 閱讀:99 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹iOS應(yīng)用程序生命周期的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

開發(fā)應(yīng)用程序都要了解其生命周期。

今天我們接觸一下iOS應(yīng)用程序的生命周期, iOS的入口在main.m文件:

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

main函數(shù)的兩個(gè)參數(shù),iOS中沒有用到,包括這兩個(gè)參數(shù)是為了與標(biāo)準(zhǔn)ANSI C保持一致。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 * __nullable principalClassName, NSString * __nullable delegateClassName);

如果主要類(principal class)為nil,將從Info.plist中獲取,如果Info.plist中不存在對應(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)生成的。

應(yīng)用程序的生命周期(AppDelegate.m):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  // Override point for customization after application launch. 
  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"); 
}

1、application didFinishLaunchingWithOptions:當(dāng)應(yīng)用程序啟動(dòng)時(shí)執(zhí)行,應(yīng)用程序啟動(dòng)入口,只在應(yīng)用程序啟動(dòng)時(shí)執(zhí)行一次。若用戶直接啟動(dòng),lauchOptions內(nèi)無數(shù)據(jù),若通過其他方式啟動(dòng),lauchOptions包含對應(yīng)方式的內(nèi)容。

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 方法相對應(yīng)。

5、applicationDidBecomeActive:在應(yīng)用程序已被激活后,要執(zhí)行的委托調(diào)用,剛好與applicationDidEnterBackground 方法相對應(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

iOS應(yīng)用程序生命周期的示例分析

程序中沒有設(shè)置UIApplicationExitsOnSuspend的值,程序不會(huì)徹底退出。

以上是“iOS應(yīng)用程序生命周期的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI