iOS开发:应用生命周期

  • iOS应用通过委托对象AppDelegate类在应用周期的不同阶段会回调不同的方法,应用周期分为以下五种状态:

iOS开发:应用生命周期_第1张图片

 

    •   Not Running(非运行状态)。应用没有运行或被系统终止。  

    •   Inactive(前台非活动状态)。应用正在进入前台状态,但是还不能接受事件处理。

    •   Active(前台活动状态)。应用进入前台状态,能接受事件处理。

    •   Background(后台状态)。应用进入后台后,依然能够执行代码。如果有可执行的代码,就会执行代码,

       如果没有可执行的代码或者将可执行的代码执行完毕,应用会马上进入挂起状态。
      
    •   Suspended(挂起状态)。处于挂起的应用进入一种“冷冻”状态,不能执行代码。如果系统内存不够,

      应用会被终止。 状态跃迁过程中应用回调的方法和本地通知

 

  •   应用状态改变的回调方法和本地通知
方法 本地通知 说明

application:didFinishLaun- chingWithOptions: 

UIApplicationDidFinishLaunching- Notification 

应用启动并进行初始化时会调用该方法并发 出通知。这个阶段会实例化根视图控制器 

applicationDidBecomeActive: 

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:.
    }


}

 

你可能感兴趣的:(iOS开发:应用生命周期)