iOS 应用程序启动的过程中的一系列活动会影响应用的加载时间,为了获得更好的应用体验,对App应用程序的的生命周期有所了解是必要的.
启动过程
App启动和进入后台如下图所示:
iOS开发人员应该对AppDelegate中的didFinishLaunchingWithOptions启动方法不会陌生:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FlyElephant return true }
这是整个App最核心的位置,不能有任何的错误,尤其是不能发生崩溃,否则只能等重新发版来解决,如果用户基数比较大,只能是祈祷保佑了.
didFinishLaunchingWithOptions一般都基础功能设置,项目开发中常见设置包括崩溃报告上传,网络请求设置,UI初始化,网络请求缓存设置,用户登录状态判断,业务第三方SDK初始化.
App的启动时间与公司的业务关联性很大,如果App启动时间太长,可以对启动过程配置过程分出必须初始化和非必须初始化的业务,对于一些不必在主线程中执行的任务,可以在子线程中异步执行.
iOS最常见的常见的操作是双击Home键应用进入前后台切换,执行代码如下:
` 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 active state; here you can undo many of the changes made on entering the background.
}`
外部链接进入App期望跳转到指定页面的执行方法(微信,支付宝支付的回调):
` func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
}`
App在每次版本更新之后升级启动需要注意是否处理上个版本的缓存,数据是否同步,版本号更新以及数据库升级.
推送通知
iOS消息推送在delegate中的执行的方法:
` func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
`
ApplicationState的三种状态激活状态,待激活状态(正在进入didFinishLaunchingWithOptions,运行在前台没有接收事件),后台状态.
`public enum UIApplicationState : Int {
case active
case inactive
case background
}`
当app启动时,首先由not running状态切换到inactive状态,此时调用application:didFinishLaunchingWithOptions:方法,然后由inactive状态切换到active状态,此时调用applicationDidBecomeActive:方法.
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. }
当app发生中断时,由active状态切换到inactive状态,此时调用applicationWillResignActive:方法.
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 invalidate graphics rendering callbacks. Games should use this method to pause the game. }
当切换到另一个app时,由状态active切换到inactive,此时调用applicationWillResignActive:方法,然后从inactive状态切换到running状态,此时调用applicationDidEnterBackground:方法.
当切换到本来的app时,由running状态切换到inactive状态,此时调用applicationWillEnterForeground:方法,然后由inactive状态切换到active状态,调用applicationDidBecomeActive:方法.
应用在didReceiveRemoteNotification回调接受通知,处理通知事件.
App在运行状态中会有各种不同的状态随时准备切换,针对不同的状态可以进行事件处理,完善应用程序的体验.
终止
iOS系统常常是为其他app启动时由于内存不足而回收内存最后需要终止应用程序,但有时也会是由于app很长时间才响应而终止.如果app当时运行在后台并且没有暂停,系统会在应用程序终止之前调用applicationWillTerminate:来保存用户的一些重要数据以便下次启动时恢复到app原来的状态.