在 AppDelegate 里面茄袖,代理方法
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
是App進程被殺死的時候的代理方法烛卧,在這里開發(fā)者可以處理相應(yīng)的業(yè)務(wù)邏輯晃痴。
在實際應(yīng)用過程中曹铃,App進程被殺死有兩種可能:
1.手動殺掉進程(雙擊Home鍵缰趋,出現(xiàn)多任務(wù)界面,上滑需要殺掉的App)。
2.系統(tǒng)殺掉進程(App在后臺的時候:若手機內(nèi)存不足秘血,系統(tǒng)會自動殺掉進程味抖;若手機內(nèi)存正常,則測試時間約180秒后灰粮,系統(tǒng)殺掉進程)仔涩。
手動殺死進程的時候,會執(zhí)行applicationWillTerminate: 方法粘舟。
系統(tǒng)殺死進程的時候熔脂,需要在applicationDidEnterBackground: 方法中添加一句代碼
- (void)applicationDidEnterBackground:(UIApplication *)application {
// 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.
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(){}];
}
這樣在正常的情況下,app進程被殺死柑肴,就會響應(yīng)applicationWillTerminate: 了霞揉。
不正常的情況就是程序崩潰,就不會響應(yīng)這個方法了晰骑,那就需要單獨處理崩潰情況下的業(yè)務(wù)邏輯了适秩。