iOS開發:應用生命週期

  • iOS應用經過委託對象AppDelegate類在應用週期的不一樣階段會回調不一樣的方法,應用週期分爲如下五種狀態:

 

    •   Not Running(非運行狀態)。應用沒有運行或被系統終止。  數據庫

    •   Inactive(前臺非活動狀態)。應用正在進入前臺狀態,可是還不能接受事件處理。併發

    •   Active(前臺活動狀態)。應用進入前臺狀態,能接受事件處理。app

    •   Background(後臺狀態)。應用進入後臺後,依然可以執行代碼。若是有可執行的代碼,就會執行代碼,ide

       若是沒有可執行的代碼或者將可執行的代碼執行完畢,應用會立刻進入掛起狀態。
      
    •   Suspended(掛起狀態)。處於掛起的應用進入一種「冷凍」狀態,不能執行代碼。若是系統內存不夠,ui

      應用會被終止。 狀態躍遷過程當中應用回調的方法和本地通知this

 

  •   應用狀態改變的回調方法和本地通知
方法 本地通知 說明

application:didFinishLaun- chingWithOptions: spa

UIApplicationDidFinishLaunching- Notification rest

應用啓動並進行初始化時會調用該方法併發 出通知。這個階段會實例化根視圖控制器 code

applicationDidBecomeActive: orm

UIApplicationDidBecomeActive- Notification

應用進入前臺並處於活動狀態時調用該方法 併發出通知。這個階段能夠恢復UI的狀態(例 如遊戲狀態等)

applicationWillResignActive: 

UIApplicationWillResignActive- Notification

應用從活動狀態進入到非活動狀態時調用該 方法併發出通知。這個階段能夠保存UI的狀 態(例如遊戲狀態等)

applicationDidEnterBackground:

UIApplicationDidEnterBackground- Notification

應用進入後臺時調用該方法併發出通知。這 個階段能夠保存用戶數據,釋放一些資源(例 如釋放數據庫資源等)

 applicationWillEnterForeground:  

UIApplicationWillEnterForeground- Notification

應用進入到前臺,可是尚未處於活動狀態 時調用該方法併發出通知。這個階段能夠恢 複用戶數據

applicationWillTerminate: UIApplicationWillTerminate- Notification 應用被終止時調用該方法併發出通知,但內 存清除時除外。這個階段釋放一些資源,也 能夠保存用戶數據 

 

  • 具體代碼可查看AppDelegate
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // 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.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // 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.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // 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.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // 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.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}
相關文章
相關標籤/搜索