IOS應用的啓動流程及其調用關係

開發中不採用storyboard創建應用時,iOS將遵循以下的調用順序啓動應用。

瞭解調用順序可以很好的把握什麼時候裝載什麼數據或者頁面。


對於採用了storyboard的應用,UIApplicationMain()將會額外加載應用的主要storyboard文件,從而創建窗口和初始視圖。

Main函數在main.m中是應用的起點,其代碼如下:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

main()採用了新的@autoreleasepool{}函數,支持LLVM3。

autorelease的作用是在新版本中體現了不在所有的Object都需需要手工release,因此新的版本代碼中沒有[object release]。

在mian函數中調用了UIApplicationMain函數

int UIApplicationMain{

      int argc,

      char *argv[],

      NSString *principalClassName,

      NSString * delegateClassName

}

前兩個參數和一般語言的入口main函數參數相同,第一個參數表示參數的個數,第二個參數表示裝載函數的數組,

第三個參數,是UIApplication類名或其子類名,若是nil,則默認使用UIApplication類名。

第四個參數是協議UIApplicationDelegate的實例化對象名,如果是nil,則從main nib文件中加載委託對象。

這個對象就是UIApplication對象監聽到系統變化的時候通知其執行的相應方法。

在我們生成的應用程序中,第三個和第四個參數都爲nil,那就是UIApplication和HelloWorldAppDelegate了

需要注意的是第四個參數的變化,之前是nil,而這裏已經更改了。第四個參數代表該應用的Delegate初始化的類名,如果從應用的主nib文件加載代理對象,則將該參數設置爲nil。

顯然,我們的應用代理將不會由之前的MainWindow.xib加載,而是直接由UIApplicationMain()函數創建。

實際上,項目中已經不再有MainWindow.xib文件。

在Xcode4.2以及以後的版本去掉main xib文件的原因很可能是storyboard的導入。storybo基於試圖控制器,而非視圖或窗口。

AppDelegate實現了UIApplicationDelegate協議,可以重寫其所有方法。

我們在打開AppDelegate文件,我們會看到這樣的代碼:

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions {
<span style="white-space:pre">	</span>self.window.rootViewController = self.viewController;
<span style="white-space:pre">	</span>[self.window makeKeyAndVisible];
<span style="white-space:pre">	</span>return YES;
}

每個應用程序有個UIWindow,這window負責管理和協調應用程序的屏幕顯示。

在這裏把ViewController實例賦值給window的rootViewController,rootViewController的view將會作爲UIWindow的首視圖。

好的,我們就可以在HelloWorldViewController的loadView裏添加我們需要的控件了。

總結程序的啓動過程如下:

1.程序入口main函數創建UIApplication實例和UIApplication代理實例。

2.在UIApplication代理實例中重寫啓動方法,設置第一ViewController。

3.在第一ViewController中添加控件,實現應用程序界面。

在UIApplicationDelegate協議定義的方法說明:

- (void)applicationWillResignActive:(UIApplication *)application
//說明:當應用程序將要入非活動狀態執行,在此期間,應用程序不接收消息或事件,比如來電話了
- (void)applicationDidBecomeActive:(UIApplication *)application
//說明:當應用程序入活動狀態執行,這個剛好跟上面那個方法相反
- (void)applicationDidEnterBackground:(UIApplication *)application
//說明:當程序被推送到後臺的時候調用。所以要設置後臺繼續運行,則在這個函數裏面設置即可
- (void)applicationWillEnterForeground:(UIApplication *)application
//說明:當程序從後臺將要重新回到前臺時候調用,這個剛好跟上面的那個方法相反。
- (void)applicationWillTerminate:(UIApplication *)application
//說明:當程序將要退出是被調用,通常是用來保存數據和一些退出前的清理工作。這個需要要設置UIApplicationExitsOnSuspend的鍵值。
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
//說明:iPhone設備只有有限的內存,如果爲應用程序分配了太多內存操作系統會終止應用程序的運行,在終止前會執行這個方法,通常可以在這裏進行內存清理工作防止程序被終止
- (void)applicationSignificantTimeChange:(UIApplication*)application
//說明:當系統時間發生改變時執行
- (void)applicationDidFinishLaunching:(UIApplication*)application
//說明:當程序載入後執行
- (void)application:(UIApplication)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame
//說明:當StatusBar框將要變化時執行
- (void)application:(UIApplication*)application willChangeStatusBarOrientation:
(UIInterfaceOrientation)newStatusBarOrientation
duration:(NSTimeInterval)duration
//說明:當StatusBar框方向將要變化時執行
- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url
//說明:當通過url執行
- (void)application:(UIApplication*)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation
//說明:當StatusBar框方向變化完成後執行
- (void)application:(UIApplication*)application didChangeSetStatusBarFrame:(CGRect)oldStatusBarFrame
//說明:當StatusBar框變化完成後執行

Monitoring App State Changes

  • – application:willFinishLaunchingWithOptions:
  • – application:didFinishLaunchingWithOptions:
  • – applicationDidBecomeActive:
  • – applicationWillResignActive:
  • – applicationDidEnterBackground:
  • – applicationWillEnterForeground:
  • – applicationWillTerminate:
  • – applicationDidFinishLaunching:

Providing a Window for Storyboarding

  •   window  property

Managing the Default Interface Orientations

  • – application:supportedInterfaceOrientationsForWindow:

Downloading Data in the Background

  • – application:performFetchWithCompletionHandler:
  • – application:handleEventsForBackgroundURLSession:completionHandler:

Handling Remote Notifications

  • – application:didReceiveRemoteNotification:
  • – application:didReceiveRemoteNotification:fetchCompletionHandler:
  • – application:didRegisterForRemoteNotificationsWithDeviceToken:
  • – application:didFailToRegisterForRemoteNotificationsWithError:

Handling Local Notifications

  • – application:didReceiveLocalNotification:

Responding to System Notifications

  • – applicationDidReceiveMemoryWarning:
  • – applicationSignificantTimeChange:

Managing App State Restoration

  • – application:shouldSaveApplicationState:
  • – application:shouldRestoreApplicationState:
  • – application:viewControllerWithRestorationIdentifierPath:coder:
  • – application:willEncodeRestorableStateWithCoder:
  • – application:didDecodeRestorableStateWithCoder:

Opening a URL Resource

  • – application:openURL:sourceApplication:annotation:
  • – application:handleOpenURL:

Managing Status Bar Changes

  • – application:willChangeStatusBarOrientation:duration:
  • – application:didChangeStatusBarOrientation:
  • – application:willChangeStatusBarFrame:
  • – application:didChangeStatusBarFrame:

Responding to Content Protection Changes

  • – applicationProtectedDataWillBecomeUnavailable:
  • – applicationProtectedDataDidBecomeAvailable:

Properties

window

The window to use when presenting a storyboard.

@property(nonatomic, retain) UIWindow *window
UIApplication.h

Instance Methods

application:didChangeStatusBarFrame:

Tells the delegate when the frame of the status bar has changed.

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame

application:didChangeStatusBarOrientation:

Tells the delegate when the interface orientation of the status bar has changed.

- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation

application:didDecodeRestorableStateWithCoder:

Tells your delegate to restore any high-level state information as part of the state restoration process.

- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder

application:didFailToRegisterForRemoteNotificationsWithError:

Sent to the delegate when Apple Push Service cannot successfully complete the registration process.

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

application:didFinishLaunchingWithOptions:

Tells the delegate that the launch process is almost done and the app is almost ready to run.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

application:didReceiveLocalNotification:

Sent to the delegate when a running app receives a local notification.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

application:didReceiveRemoteNotification:

Tells the delegate that the running app received a remote notification.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

application:didReceiveRemoteNotification:fetchCompletionHandler:

Tells the app that a push notification arrived that indicates there is data to be fetched.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResultresult))handler

application:didRegisterForRemoteNotificationsWithDeviceToken:

Tells the delegate that the app successfully registered with Apple Push Service (APS).

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

application:handleEventsForBackgroundURLSession:completionHandler:

Tells the delegate that events related to a URL session are waiting to be processed.

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler

application:handleOpenURL:

Asks the delegate to open a resource identified by URL. (Deprecated. Use the application:openURL:sourceApplication:annotation: method instead.)

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

application:openURL:sourceApplication:annotation:

Asks the delegate to open a resource identified by URL.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

application:performFetchWithCompletionHandler:

Tells the app that it can begin a fetch operation if it has data to download.

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

application:shouldRestoreApplicationState:

Asks the delegate whether the app’s saved state information should be restored.

- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder

application:shouldSaveApplicationState:

Asks the delegate whether the app’s state should be preserved.

- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder

application:supportedInterfaceOrientationsForWindow:

Asks the delegate for the interface orientations to use for the view controllers in the specified window.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

application:viewControllerWithRestorationIdentifierPath:coder:

Asks the delegate to provide the specified view controller.

- (UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder

application:willChangeStatusBarFrame:

Tells the delegate when the frame of the status bar is about to change.

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame

application:willChangeStatusBarOrientation:duration:

Tells the delegate when the interface orientation of the status bar is about to change.

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration

application:willEncodeRestorableStateWithCoder:

Tells your delegate to save any high-level state information at the beginning of the state preservation process.

- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder

application:willFinishLaunchingWithOptions:

Tells the delegate that the launch process has begun but that state restoration has not yet occurred.

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions

applicationDidBecomeActive:

Tells the delegate that the app has become active.

- (void)applicationDidBecomeActive:(UIApplication *)application

applicationDidEnterBackground:

Tells the delegate that the app is now in the background.

- (void)applicationDidEnterBackground:(UIApplication *)application

applicationDidFinishLaunching:

Tells the delegate when the app has finished launching.

- (void)applicationDidFinishLaunching:(UIApplication *)application

applicationDidReceiveMemoryWarning:

Tells the delegate when the app receives a memory warning from the system.

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application

applicationProtectedDataWillBecomeUnavailable:

Tells the delegate that the protected files are about to become unavailable.

- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application

applicationSignificantTimeChange:

Tells the delegate when there is a significant change in the time.

- (void)applicationSignificantTimeChange:(UIApplication *)application

applicationWillEnterForeground:

Tells the delegate that the app is about to enter the foreground.

- (void)applicationWillEnterForeground:(UIApplication *)application

applicationWillResignActive:

Tells the delegate that the app is about to become inactive.

- (void)applicationWillResignActive:(UIApplication *)application

applicationWillTerminate:

- (void)applicationWillTerminate:(UIApplication *)application

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章