iOS 應(yīng)用程序啟動(dòng)的過(guò)程中的一系列活動(dòng)會(huì)影響應(yīng)用的加載時(shí)間志膀,為了獲得更好的應(yīng)用體驗(yàn)触幼,對(duì)App應(yīng)用程序的的生命周期有所了解是必要的.
啟動(dòng)過(guò)程
App啟動(dòng)和進(jìn)入后臺(tái)如下圖所示:
iOS開(kāi)發(fā)人員應(yīng)該對(duì)AppDelegate中的didFinishLaunchingWithOptions啟動(dòng)方法不會(huì)陌生:
<pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FlyElephant return true }
</code></pre>
這是整個(gè)App最核心的位置类垦,不能有任何的錯(cuò)誤藕甩,尤其是不能發(fā)生崩潰剩瓶,否則只能等重新發(fā)版來(lái)解決锋叨,如果用戶(hù)基數(shù)比較大菊匿,只能是祈禱保佑了.
didFinishLaunchingWithOptions一般都基礎(chǔ)功能設(shè)置付呕,項(xiàng)目開(kāi)發(fā)中常見(jiàn)設(shè)置包括崩潰報(bào)告上傳,網(wǎng)絡(luò)請(qǐng)求設(shè)置跌捆,UI初始化徽职,網(wǎng)絡(luò)請(qǐng)求緩存設(shè)置,用戶(hù)登錄狀態(tài)判斷佩厚,業(yè)務(wù)第三方SDK初始化.
App的啟動(dòng)時(shí)間與公司的業(yè)務(wù)關(guān)聯(lián)性很大姆钉,如果App啟動(dòng)時(shí)間太長(zhǎng),可以對(duì)啟動(dòng)過(guò)程配置過(guò)程分出必須初始化和非必須初始化的業(yè)務(wù)可款,對(duì)于一些不必在主線(xiàn)程中執(zhí)行的任務(wù)育韩,可以在子線(xiàn)程中異步執(zhí)行.
iOS最常見(jiàn)的常見(jiàn)的操作是雙擊Home鍵應(yīng)用進(jìn)入前后臺(tái)切換,執(zhí)行代碼如下:
<pre><code>` 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.
}`</code></pre>
外部鏈接進(jìn)入App期望跳轉(zhuǎn)到指定頁(yè)面的執(zhí)行方法(微信闺鲸,支付寶支付的回調(diào)):
<pre><code>` func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
}`</code></pre>
App在每次版本更新之后升級(jí)啟動(dòng)需要注意是否處理上個(gè)版本的緩存筋讨,數(shù)據(jù)是否同步,版本號(hào)更新以及數(shù)據(jù)庫(kù)升級(jí).
推送通知
iOS消息推送在delegate中的執(zhí)行的方法:
<pre><code>` func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
`</code></pre>
ApplicationState的三種狀態(tài)激活狀態(tài)摸恍,待激活狀態(tài)(正在進(jìn)入didFinishLaunchingWithOptions,運(yùn)行在前臺(tái)沒(méi)有接收事件)悉罕,后臺(tái)狀態(tài).
<pre><code>`public enum UIApplicationState : Int {
case active
case inactive
case background
}`</code></pre>
當(dāng)app啟動(dòng)時(shí)赤屋,首先由not running狀態(tài)切換到inactive狀態(tài),此時(shí)調(diào)用application:didFinishLaunchingWithOptions:方法壁袄,然后由inactive狀態(tài)切換到active狀態(tài)类早,此時(shí)調(diào)用applicationDidBecomeActive:方法.
<pre><code>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. }
</code></pre>
當(dāng)app發(fā)生中斷時(shí),由active狀態(tài)切換到inactive狀態(tài)嗜逻,此時(shí)調(diào)用applicationWillResignActive:方法.
<pre><code>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. }
</code></pre>
當(dāng)切換到另一個(gè)app時(shí)涩僻,由狀態(tài)active切換到inactive,此時(shí)調(diào)用applicationWillResignActive:方法栈顷,然后從inactive狀態(tài)切換到running狀態(tài)逆日,此時(shí)調(diào)用applicationDidEnterBackground:方法.
當(dāng)切換到本來(lái)的app時(shí),由running狀態(tài)切換到inactive狀態(tài)萄凤,此時(shí)調(diào)用applicationWillEnterForeground:方法室抽,然后由inactive狀態(tài)切換到active狀態(tài),調(diào)用applicationDidBecomeActive:方法.
應(yīng)用在didReceiveRemoteNotification回調(diào)接受通知靡努,處理通知事件.
App在運(yùn)行狀態(tài)中會(huì)有各種不同的狀態(tài)隨時(shí)準(zhǔn)備切換坪圾,針對(duì)不同的狀態(tài)可以進(jìn)行事件處理,完善應(yīng)用程序的體驗(yàn).
終止
iOS系統(tǒng)常常是為其他app啟動(dòng)時(shí)由于內(nèi)存不足而回收內(nèi)存最后需要終止應(yīng)用程序惑朦,但有時(shí)也會(huì)是由于app很長(zhǎng)時(shí)間才響應(yīng)而終止.如果app當(dāng)時(shí)運(yùn)行在后臺(tái)并且沒(méi)有暫停兽泄,系統(tǒng)會(huì)在應(yīng)用程序終止之前調(diào)用applicationWillTerminate:來(lái)保存用戶(hù)的一些重要數(shù)據(jù)以便下次啟動(dòng)時(shí)恢復(fù)到app原來(lái)的狀態(tài).