UIApplication的方法

一、iOS程序的啓動執行順序

@interface AppDelegate : UIResponder <UIApplicationDelegate>

1 程序的入口

進入main函數, 設置AppDelegate稱爲函數的代理

2 程序完成加載

-[AppDelegate application:didFinishLaunchingWithOptions:]

3 創建window窗口

4 程序被激活

-[AppDelegate applicationDidBecomeActive:]

5 當點擊command+H時

程序取消激活狀態

-[AppDelegate applicationWillResignActive:]

程序進入後臺

-[AppDelegate applicationDidEnterBackground:]

6 點擊進入工程

程序進入前臺

-[AppDelegate applicationWillEnterForeground:]

程序被激活

-[AppDelegate applicationDidBecomeActive:]

代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption{// Override point for customization after application launch.
    NSLog(@"didFinishLaunchingWithOptions");
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application { /* 當應用程序從活動狀態(active)變到非活動狀態(inactive時被觸發調用, 這可能發生在一些臨時中斷下(例如:來電話、來短信)又或者程序退出時,他會先過渡到後臺然後terminate 使用這方法去暫停正在進行的任務,禁用計時器,節流OpenGL ES 幀率。在遊戲中應該在這個方法裏面暫停遊戲。 */
    // 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 { /* 使用這種方法來釋放共享資源,保存用戶數據,無效計時器,存儲足夠多的應用程序狀態信息來恢復您的應用程序的當前狀態,以防它終止丟失數據。 如果你的程序支持後臺運行,那麼當用戶退出時不會調用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 { /* 先從後臺切換到非活動狀態,然後進入活動狀態。 */
    // 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 { /* 重啓所有的任務,不管是從非活動狀態還是剛啓動程序,還是後臺狀態。 */
    // 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");
}

測試:
下面給出打印就明白他們之間的交互先後順序了:

啓動程序

2014-07-28 15:22:39.883 LifeCycle[3024:a0b] didFinishLaunchingWithOptions

2014-07-28 15:22:39.887 LifeCycle[3024:a0b] DidBecomeActive

按下Home鍵

2014-07-28 15:22:43.130 LifeCycle[3024:a0b] WillResignActive

2014-07-28 15:22:43.131 LifeCycle[3024:a0b] DidEnterBackground

重新點擊程序

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] WillEnterForeground

2014-07-28 15:22:44.380 LifeCycle[3024:a0b] DidBecomeActive

分析
1.application:didFinishLaunchingWithOptions:

程序首次已經完成啓動時執行,若直接啓動,launchOptions中沒有數據;否則,launchOptions將包含對應方式的內容(比如從微信中啓動節奏大師–)。

2.applicationWillResignActive(非活動)

程序將要失去Active狀態時調用,比如按下Home鍵或有電話信息進來。之後程序將進入後臺狀態。對應的applicationWillEnterForeground這個方法用來

a、暫停正在執行的任務;

b、禁止計時器;

c、減少OpenGL ES幀率;

d、若爲遊戲應暫停遊戲;

3.applicationDidEnterBackground(已經進入後臺)

程序已經進入後臺時調用,對應applicationDidBecomeActive(已經變成前臺),這個方法用來

a、釋放共享資源;

b、保存用戶數據(寫到硬盤);

c、作廢計時器;

d、保存足夠的程序狀態以便下次恢復;

4.applicationWillEnterForeground(將進入前臺)

程序即將進去前臺時調用,對應applicationWillResignActive(將進入後臺)。這個方法用來

1.撤銷applicationWillResignActive中做的改變。

5.applicationDidBecomeActive(已經進入前臺)

程序已經變爲Active(前臺)時調用。對應applicationDidEnterBackground(已經進入後臺)。

1.若程序之前在後臺,在此方法內刷新用戶界面。

6.applicationWillTerminate

程序即將退出時調用。記得保存數據,如applicationDidEnterBackground方法一樣。

利用以下通知 可以實現自己的代碼

UIKIT_EXTERN NSNotificationName const UIApplicationDidEnterBackgroundNotification       NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN NSNotificationName const UIApplicationWillEnterForegroundNotification      NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN NSNotificationName const UIApplicationDidFinishLaunchingNotification;
UIKIT_EXTERN NSNotificationName const UIApplicationDidBecomeActiveNotification;
UIKIT_EXTERN NSNotificationName const UIApplicationWillResignActiveNotification;
UIKIT_EXTERN NSNotificationName const UIApplicationDidReceiveMemoryWarningNotification;
UIKIT_EXTERN NSNotificationName const UIApplicationWillTerminateNotification;

二 其他方法

1.關閉遠程通知的推送方法

unregisterForRemoteNotifications

2.防止屏幕睡眠

[[UIApplication sharedApplication].idleTimerDisabled=YES;  

3.打開XXX

openURL
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章