開發中不採用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.
UIApplication.h
Instance Methods
application:didChangeStatusBarFrame:
Tells the delegate when the frame of the status bar has changed.
application:didChangeStatusBarOrientation:
Tells the delegate when the interface orientation of the status bar has changed.
application:didDecodeRestorableStateWithCoder:
Tells your delegate to restore any high-level state information as part of the state restoration process.
application:didFailToRegisterForRemoteNotificationsWithError:
Sent to the delegate when Apple Push Service cannot successfully complete the registration process.
application:didFinishLaunchingWithOptions:
Tells the delegate that the launch process is almost done and the app is almost ready to run.
application:didReceiveLocalNotification:
Sent to the delegate when a running app receives a local notification.
application:didReceiveRemoteNotification:
Tells the delegate that the running app received a remote notification.
application:didReceiveRemoteNotification:fetchCompletionHandler:
Tells the app that a push notification arrived that indicates there is data to be fetched.
application:didRegisterForRemoteNotificationsWithDeviceToken:
Tells the delegate that the app successfully registered with Apple Push Service (APS).
application:handleEventsForBackgroundURLSession:completionHandler:
Tells the delegate that events related to a URL session are waiting to be processed.
application:handleOpenURL:
Asks the delegate to open a resource identified by URL. (Deprecated. Use the application:openURL:sourceApplication:annotation:
method instead.)
application:openURL:sourceApplication:annotation:
Asks the delegate to open a resource identified by URL.
application:performFetchWithCompletionHandler:
Tells the app that it can begin a fetch operation if it has data to download.
application:shouldRestoreApplicationState:
Asks the delegate whether the app’s saved state information should be restored.
application:shouldSaveApplicationState:
Asks the delegate whether the app’s state should be preserved.
application:supportedInterfaceOrientationsForWindow:
Asks the delegate for the interface orientations to use for the view controllers in the specified window.
application:viewControllerWithRestorationIdentifierPath:coder:
Asks the delegate to provide the specified view controller.
application:willChangeStatusBarFrame:
Tells the delegate when the frame of the status bar is about to change.
application:willChangeStatusBarOrientation:duration:
Tells the delegate when the interface orientation of the status bar is about to change.
application:willEncodeRestorableStateWithCoder:
Tells your delegate to save any high-level state information at the beginning of the state preservation process.
application:willFinishLaunchingWithOptions:
Tells the delegate that the launch process has begun but that state restoration has not yet occurred.
applicationDidBecomeActive:
Tells the delegate that the app has become active.
applicationDidEnterBackground:
Tells the delegate that the app is now in the background.
applicationDidFinishLaunching:
Tells the delegate when the app has finished launching.
applicationDidReceiveMemoryWarning:
Tells the delegate when the app receives a memory warning from the system.
applicationProtectedDataWillBecomeUnavailable:
Tells the delegate that the protected files are about to become unavailable.
applicationSignificantTimeChange:
Tells the delegate when there is a significant change in the time.
applicationWillEnterForeground:
Tells the delegate that the app is about to enter the foreground.
applicationWillResignActive:
Tells the delegate that the app is about to become inactive.
applicationWillTerminate:
- (void)applicationWillTerminate:(UIApplication *)application