iOS程式啟動過程

來源:互聯網
上載者:User

標籤:

  1. First, the function creates the main application object (step 3 in the flowchart). If you specify nil as the third argument to UIApplicationMain() (the default), it will create an instance of UIApplication in this step. This is usually what you want. However, if you need to subclass UIApplication (for example, to override its event handling insendEvent:), you have to pass a string with the name of your subclass toUIApplicationMain().

  2. The function then looks at its fourth argument. If it is non-nil, it interprets it as the name of the class for the application delegate, instantiates an object of this class and assigns it as the application object’s delegate. The default for the fourth argument is nil, though, which signifies that the app delegate will be created in the main NIB file.

  3. Next, UIApplicationMain() loads and parses your app’s Info.plist (step 4). If it contains a key named “Main nib file base name” (NSMainNibFile), the function will also load the NIB file specified there (step 5).

  4. By default, the main NIB file is called MainWindow.nib. It contains at least an object representing the application delegate, connected to the File’s Owner’s delegate outlet (step 6), and a UIWindow object that will be used as the app’s main window, connected to an outlet of the app delegate. If you used a view-controller-based app template, the NIB file will also contain your app’s root view controller and possibly one or more view child controllers.

    It is worth mentioning that this is the only step where the UIKit-based app templates (Window-based, View-based, Navigation-based, Tab-based, etc.) differ significantly from each other. If you started out with a view-based app and later want to introduce a navigation controller, there is no need to start a new project: simply replace the root view controller in the main NIB file and adjust one or two lines of code in the app delegate. I noticed that many newbies to the iOS platform struggle with this problem and assume a huge difference between the different project templates. There isn’t.

  5. Now, UIApplicationMain() creates the application’s run loop that is used by theUIApplication instance to process events such as touches or network events (step 7). The run loop is basically an infinite loop that causes UIApplicationMain() to never return.

  6. Before the application object processes the first event, it finally sends the well-knownapplication:didFinishLaunchingWithOptions: message to its delegate, giving us the chance to do our own setup (step 8). The least we have to do here is put our main window on the screen by sending it a makeKeyAndVisible message.

// 這是原文我摘抄的部分,英語好的可以看看,下面說說自己的理解

1 main函數

int main(int argc, char * argv[]) {    NSLog(@"===%s",argv[0]);    @autoreleasepool {        /// 函數原型:        //  int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));    }}

這裡面的代碼一般是不變的。首先是一個自動釋放池,保證結束時記憶體釋放,下面是參數介紹:

  argc, argv:是標C的參數,argc為argv數組中元素的個數。argv一般有一個元素argv[0]即當前可執行程式的路徑。(另外,在linux系統下我們通過終端開啟一個程式可以給它傳遞參數,具體不再展開。如果不知所云括弧裡面的自動忽略);

2 UIApplicationMain

   1)根據傳進的參數建立UIApplication對象;

   2)根據傳進的參數建立UIApplication的delegate對象,並將該delegate對象賦值給UIApplication對象中的delegate屬性。

   3)開啟一個訊息迴圈

具體闡述:

main函數中執行了一個UIApplicationMain這個函數

int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName); 
argc、argv:直接傳遞給UIApplicationMain進行相關處理即可

principalClassName:指定應用程式類名(app的象徵),該類必須是UIApplication(或子類)。如果為nil,則用UIApplication類作為預設值,它是一個單例,代表一個進程,也是程式建立的第一個對象,利用UIApplication對象,能進行一些應用層級的操作;

 

delegateClassName:指定應用程式的代理類,該類必須遵守UIApplicationDelegate協議

UIApplicationMain函數會根據principalClassName建立UIApplication對象,根據delegateClassName建立一個delegate對象,並將該delegate對象賦值給UIApplication對象中的delegate屬性

接著會建立應用程式的Main Runloop(事件迴圈),進行事件的處理(首先會在程式完畢後調用delegate對象的application:didFinishLaunchingWithOptions:方法)

程式正常退出時UIApplicationMain函數才返回

 

下面分為storyboard啟動和沒有storyboard啟動

 

 

##有storyboard##

3 根據Info.plist載入storyboard

   1)建立UIWindow,UIWindow是一種特殊的UIView,通常在一個App中只會有一個UIWindow(注意是通常,還有其它的,比如彈出的鍵盤)。設定為主視窗,同一時刻主視窗只有一個,可以通過[UIApplication sharedApplication].keyWindow擷取。

   2)建立和設定UIWindow的rootViewController。

   3)顯示視窗

具體闡述:

根據Info.plist獲得最主要storyboard的檔案名稱,載入最主要的storyboard(有storyboard

- 建立UIWindow 
- 建立和設定UIWindow的rootViewController(storyboard中箭頭指向的控制器) 
- 顯示視窗 
有storyboard:官方文檔:iosX /uikit/viewController p c for ios
The Main Storyboard Initializes Your App’s User Interface 
The main storyboard is defined in the app’s Information property list file. If a main storyboard is declared in this file, then when your app launches, iOS performs the following steps:

    • It instantiates a window for you.
    • It loads the main storyboard and instantiates its initial view controller.
    • It assigns the new view controller to the window’s rootViewController property and then makes the window visible on the screen.

 翻譯:

      • 主故事面板初始化應用程式的使用者介面
        主故事面板中定義的應用程式的資訊屬性列表檔案。如果一個主要的故事是在這個info.plist檔案中聲明(main),然後當你的應用程式啟動,iOS執行以下步驟:
        1.它為你執行個體化一個視窗。
        2.它載入(箭頭所指向)主故事面板並執行個體化為初始視圖控制器。
        3.它賦予新的視圖控制器視窗為RootViewController並將在螢幕上顯示這個視窗內容。

 

##沒有storyboard##

3 delegate對象開始處理(監聽)系統事件(沒有storyboard)

   1)程式啟動完畢的時候, 就會調用代理的application:didFinishLaunchingWithOptions:方法

       2)在application:didFinishLaunchingWithOptions:中建立UIWindow:window

       3)建立和設定UIWindow的rootViewController

       4)顯示並設定window為主視窗:[window makeKeyAndVisible]; 然後self.window = window;防止被釋放。

 

 

iOS程式啟動過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.