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


}
相关文章
相关标签/搜索