App啟動原理和啟動過程

來源:互聯網
上載者:User

標籤:

   一、程式啟動原理1.1、main函數中執行了一個UIApplicationMain這個函數UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);

1.2、argc、argv:直接傳遞給UIApplicationMain進行相關處理即可 

     principalClassName:指定應用程式類名(app的象徵),該類必須是UIApplication(或子類)。如果為nil,則用UIApplication類作為預設值

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

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

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

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

#import <UIKit/UIKit.h>#import"AppDelegate.h"int main(int argc, char * argv[]){    @autoreleasepool {        // return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));        // return UIApplicationMain(argc, argv, @"UIApplication", NSStringFromClass([AppDelegate class]));/*         argc: 系統或者使用者傳入的參數個數         argv: 系統或者使用者傳入的實際參數         1.根據傳入的第三個參數建立UIApplication對象         2.根據傳入的第四個產生建立UIApplication對象的代理         3.設定剛剛建立出來的代理對象為UIApplication的代理         4.開啟一個事件迴圈         */return UIApplicationMain(argc, argv, @"UIApplication", @"AppDelegate");    }} 
  二、程式啟動的完整過程

 

從main檔案開始說起程式啟動分為兩類:1.有storyboard 2.沒有storyboard

(一)有storyboard情況下:
1.main函數

2.UIApplicationMain

    - 建立UIApplication對象    - 建立UIApplication的delegate對象

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

    - 建立UIWindow    - 建立和設定UIWindow的rootViewController    - 顯示視窗

(二)沒有storyboard情況下:
1.main函數

2.UIApplicationMain

    - 建立UIApplication對象    - 建立UIApplication的delegate對象

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

    - 程式啟動完畢的時候, 就會調用代理的application:didFinishLaunchingWithOptions:方法    - 在application:didFinishLaunchingWithOptions:中建立UIWindow    - 建立和設定UIWindow的rootViewController    - 顯示視窗

 

  

App啟動原理和啟動過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.