开发中不采用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