APP的生命周期
像Android apps 一樣有生命周期刺洒。Apps就是用戶自定義的代碼,需要利用系統(tǒng)提供的資源完成特定的任務(wù)撇簿,所以了解Apps的生命周期是必要的柔袁。
APP 的main()函數(shù)
iOS系統(tǒng)是基于c 語言的,其Apps的入口當然也是main函數(shù)捣卤,不過main函數(shù)不用自己寫忍抽,有創(chuàng)建工程時自動完成。如圖下代碼:
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
這個main函數(shù)的主要作用是將控制權(quán)交給UIKit framework董朝。[UIApplicationMain] 函數(shù)負責創(chuàng)建App 核心對象鸠项,通storyboard文件中加載用戶界面,調(diào)用自定義代碼做初始化工作子姜。
APP的結(jié)構(gòu)
在啟動的期間祟绊,UIApplicationMain 函數(shù)創(chuàng)建幾個關(guān)鍵對象開始運行APP,其中最關(guān)鍵是 UIApplication對象哥捕,這個對象讓APP和系統(tǒng)交互更加便利牧抽。 如圖2,這是大多數(shù)APP含有的model-view-controller架構(gòu)模式遥赚,這個模式將用戶數(shù)據(jù)扬舒、業(yè)務(wù)邏輯從界面中分離,并有利于iOS設(shè)備的適配凫佛。`
APP的狀態(tài)
APP具有以下幾種狀態(tài)讲坎,我們主要使用到的是Active和Background狀態(tài)孕惜。在Active狀態(tài)時,用戶程序處于前臺與用戶交互處理相關(guān)事件衣赶。進入后臺時可以進行音樂播放诊赊、錄音、位置更新府瞄、等操作碧磅。下圖為APP的狀態(tài)圖:
//啟動程序是最先調(diào)用的函數(shù)
-(BOOL) application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
return YES;
}
//在app顯示給用戶之前,最后的初始化操作遵馆。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
/*通知APP將要變成前臺程序鲸郊,這里可以重啟被暫停的任務(wù),如果此程序在后臺進入可以選擇更新用戶界面货邓。
*/
- (void)applicationDidBecomeActive:(UIApplication *)application {
// 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 將進入前臺秆撮,
*這里可以撤銷一些在為后臺運行而做的改變。
**/
- (void)applicationWillEnterForeground:(UIApplication *)application {
// 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將要離開前臺换况,(依次進行inActive -》background-》-Suspended)职辨。如果有電話接入、SMS消息戈二、是用戶退出舒裤、或者想后臺切換都會調(diào)用此函數(shù)。
*應該使用這個方法暫停正在執(zhí)行的任務(wù)觉吭,取消定時器腾供、取消圖形渲染回調(diào),暫停游戲等操作鲜滩。
*/
- (void)applicationWillResignActive:(UIApplication *)application {
// 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將要進入后臺狀態(tài)伴鳖,隨時可能被掛起。
*應該使用這個方法:釋放分享的資源徙硅,存儲用戶數(shù)據(jù)榜聂、取消定時器、保存APP的狀態(tài)消息以用于恢復闷游。
*如果APP支持后臺運行峻汉,當用戶退回時,調(diào)用此方法而不調(diào)用applicationWillTerminate脐往。
**/
- (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.
}
/**
*通知 APP 將要終止休吠,如果程序被掛起將不調(diào)用。
*保存數(shù)據(jù)
**/
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}