ios 啟動運行簡述

覺得應(yīng)該從頭梳理一下

ios 作為一個移動操作系統(tǒng),擔(dān)負著管理手機資源的任務(wù),不管是文件系統(tǒng),還是進程調(diào)度,還是內(nèi)存調(diào)度,都是因為有了 ios 對底層硬件的管理,才有了上層應(yīng)用程序的順利執(zhí)行.....

作為一款操作系統(tǒng),肯定和pc平臺的操作系統(tǒng)在原理上沒有什么異同,都是加電復(fù)位寄存器(ip)地址,指向?qū)⒁M行加載的第一行程序,在 cpu 的順序執(zhí)行下加載操作系統(tǒng),然后常駐系統(tǒng)監(jiān)聽,對打開的應(yīng)用程序進行管理

我們按下應(yīng)用程序的那一刻 : 到使用起來,這之間又發(fā)生了什么呢?


眾所周知,應(yīng)用程序只有在內(nèi)存中執(zhí)行才算是真正的進程......
當(dāng)我們按下應(yīng)用程序圖標后.操作系統(tǒng)檢測到這個事件,然后通過事件傳遞處理到這個圖標,內(nèi)存開始加載資源, 保存數(shù)據(jù),IP指針指到將要運行的代碼快處,進入用戶態(tài)模式,開始執(zhí)行代碼..也就是我們寫的源文件編譯過后的二進制.......

這里就要看官方文檔了

The Main Function

The entry point for every C-based app is the main function and iOS apps are no different. What is different is that for iOS apps you do not write the main function yourself. Instead, Xcode creates this function as part of your basic project. Listing 2-1 shows an example of this function. With few exceptions, you should never change the implementation of the main function that Xcode provides.

翻譯:正如每個基于c的app一樣,ios的app進入點都是一個 main 函數(shù),不同之處在于你不需要手動書寫這個主函數(shù),xcode 會自動創(chuàng)建,也最好不要隨便改動 代碼如下:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

The main function of an iOS app
The only thing to mention about the main function is that its job is to hand control off to the UIKit framework. The UIApplicationMain function handles this process by creating the core objects of your app, loading your app’s user interface from the available storyboard files, calling your custom code so that you have a chance to do some initial setup, and putting the app’s run loop in motion. The only pieces that you have to provide are the storyboard files and the custom initialization code.

上面解釋了主函數(shù)的作用

  • 創(chuàng)造核心對象
  • 控制 uikit 層
  • 加載 storyboard 加載初始化代碼
  • 加載 runloop 監(jiān)聽

上面這個圖是我們最熟系的 mvc設(shè)計模式的官方圖片...可以清楚的看到 uiapplication 和 uiwindow 是系統(tǒng)的.所以一般不要去繼承這兩個類...

完成之后就開啟了main runloop 循環(huán) 對于 runloop 的介紹,還是看官方比較權(quán)威

The Main Run Loop

An app’s main run loop processes all user-related events. The UIApplication object sets up the main run loop at launch time and uses it to process events and handle updates to view-based interfaces. As the name suggests, the main run loop executes on the app’s main thread. This behavior ensures that user-related events are processed serially in the order in which they were received.

As the user interacts with a device, events related to those interactions are generated by the system and delivered to the app via a special port set up by UIKit. Events are queued internally by the app and dispatched one-by-one to the main run loop for execution. The UIApplication object is the first object to receive the event and make the decision about what needs to be done. A touch event is usually dispatched to the main window object, which in turn dispatches it to the view in which the touch occurred. Other events might take slightly different paths through various app objects.

Many types of events can be delivered in an iOS app.. Many of these event types are delivered using the main run loop of your app, but some are not. Some events are sent to a delegate object or are passed to a block that you provide. For information about how to handle most types of events—including touch, remote control, motion, accelerometer, and gyroscopic events
從上圖中我們可以看到 main runloop 的作用就是傳遞許多中類型事件(但不是全部),application 是第一個接收到的對象,,然后根據(jù)事件進行派遣,一些會調(diào)用自定義的 delegate 代理(protocal 實現(xiàn)),一些會調(diào)用block....但都是因為有事件派遣產(chǎn)生...

應(yīng)用程序狀態(tài)轉(zhuǎn)換

Most state transitions are accompanied by a corresponding call to the methods of your app delegate object. These methods are your chance to respond to state changes in an appropriate way. These methods are listed below, along with a summary of how you might use them.

application:willFinishLaunchingWithOptions:—This method is your app’s first chance to execute code at launch time.
application:didFinishLaunchingWithOptions:—This method allows you to perform any final initialization before your app is displayed to the user.
applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.
applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.
applicationDidEnterBackground:—Lets you know that your app is now running in the background and may be suspended at any time.
applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.
applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended.

在產(chǎn)生了main runloop 之后,發(fā)生了什么呢?

現(xiàn)在應(yīng)用程序已經(jīng)有了,但是還沒有任何可以顯示的東西...

  • 如果有 storyboard 且 info.plist 配置正確的話,系統(tǒng)會自動創(chuàng)建uiwindow,作為 appdelegate的一個屬性,自動加載 storyboard 中箭頭指向的的控 制器,作為uiwindow 的根控制器,加載視圖顯示.......(自動做了沒有的步驟而已)
  • 如果沒有的話, 就要自己在程序方法中設(shè)置
    • 程序啟動完畢的時候, 就會調(diào)用代理的application:didFinishLaunchingWithOptions:方法

    • 在application:didFinishLaunchingWithOptions:中創(chuàng)建UIWindow

    • 創(chuàng)建和設(shè)置UIWindow的rootViewController

    • 顯示窗口

之后事件循環(huán)監(jiān)聽,就如圖二一樣,開始使用......(未完)

參考:the app life cycle.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末曼验,一起剝皮案震驚了整個濱河市杯矩,隨后出現(xiàn)的幾起案子梳猪,更是在濱河造成了極大的恐慌雷蹂,老刑警劉巖赦肋,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異硬猫,居然都是意外死亡炫彩,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門斯够,熙熙樓的掌柜王于貴愁眉苦臉地迎上來囚玫,“玉大人,你說我怎么就攤上這事雳刺〗僭睿” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵掖桦,是天一觀的道長本昏。 經(jīng)常有香客問我,道長枪汪,這世上最難降的妖魔是什么涌穆? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮雀久,結(jié)果婚禮上宿稀,老公的妹妹穿的比我還像新娘。我一直安慰自己赖捌,他們只是感情好祝沸,可當(dāng)我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布矮烹。 她就那樣靜靜地躺著,像睡著了一般罩锐。 火紅的嫁衣襯著肌膚如雪奉狈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天涩惑,我揣著相機與錄音仁期,去河邊找鬼。 笑死竭恬,一個胖子當(dāng)著我的面吹牛跛蛋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播痊硕,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼赊级,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了寿桨?” 一聲冷哼從身側(cè)響起此衅,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎亭螟,沒想到半個月后挡鞍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡预烙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年墨微,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扁掸。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡翘县,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谴分,到底是詐尸還是另有隱情锈麸,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布牺蹄,位于F島的核電站忘伞,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏沙兰。R本人自食惡果不足惜氓奈,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鼎天。 院中可真熱鬧舀奶,春花似錦、人聲如沸斋射。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涧至,卻和暖如春纱兑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背化借。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留捡多,地道東北人蓖康。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像垒手,于是被迫代替她去往敵國和親蒜焊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容