覺得應(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)聽,就如圖二一樣,開始使用......(未完)