你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS 使用順序生命周期

iOS 使用順序生命周期

編輯:IOS開發綜合

開發使用順序都要理解其生命周期。

明天我們接觸一下IOS使用順序的生命周期,IOS的入口在main.m文件:

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

main函數的兩個參數,IOS中沒有用到,包括這兩個參數是為了與規范ANSI C堅持分歧。UIApplicationMain函數,前兩個和main函數一樣,重點是後兩個,官方闡明是這樣的:

// 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中不存在對應的key,則默許為UIApplication;假如代理類(delegate class)將在新建工程時創立。

依據UIApplicationMain函數,順序將進入AppDelegate.m,這個文件是xcode新建工程時自動生成的。

使用順序的生命周期(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:當使用順序啟動時執行,使用順序啟動入口,只在使用順序啟動時執行一次。若用戶直接啟動,lauchOptions內有數據,若經過其他方式啟動,lauchOptions包括對應方式的內容。

2、applicationWillResignActive:在使用順序將要由活動形態切換到非活動形態時分,要執行的委托調用,如 按下 home 按鈕,前往主屏幕,或全屏之間切換使用順序等。

3、applicationDidEnterBackground:在使用順序已進入後台順序時,要執行的委托調用。

4、applicationWillEnterForeground:在使用順序將要進入前台時(被激活),要執行的委托調用,剛好與applicationWillResignActive 辦法絕對應。

5、applicationDidBecomeActive:在使用順序已被激活後,要執行的委托調用,剛好與applicationDidEnterBackground辦法絕對應。

6、applicationWillTerminate:在使用順序要完全推出的時分,要執行的委托調用,這個需求要設置UIApplicationExitsOnSuspend的鍵值。


初次啟動:

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

點擊順序圖標進入:

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 應用程序生命周期【iOS 使用順序生命周期】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved