UIApplication
UIapplication *app = [UIApplication shareApplication];
- 一般涌来做一些应用界别的操作:app的提醒狂,联网状态,打开网页,控制状态栏。(大多为对象方法)
UIApplation 的delegate
// 学习代理方法,只需要知道这个什么时候调用,这个方法可以用来干嘛
// 程序启动完成的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s",__func__);
return YES;
}
// 当app失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"%s",__func__);
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
// app进入后台的时候调用
// app忽然打断的时候,在这里保存一些需要用到的数据
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"%s",__func__);
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
// app进入即将前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"%s",__func__);
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
// 当app获取到焦点的时候调用,意味着app可以与用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"%s",__func__);
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
// app被关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"%s",__func__);
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
// app接收到内存警告的时候调用
// 清空图片的缓存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"%s",__func__);
}
程序启动原理
// main -> UIApplicationMain
/*
UIApplicationMain
1.创建UIApplication
2.创建UIApplicationDelegate,并且成为UIApplication代理
3.开启主运行循环,保持程序一直在运行
4.加载info.plist,判断有没有指定main.stroyboard,指定了就加载
加载main.stroyboard做的事情
1.创建窗口
2.加载main.storyboard,并且加载main.storyboard指定的控制器
3.把新创建的控制器作为窗口的跟控制器,让窗口显示出来
*/
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
UIWindow
- UIWindow的创建(模拟main storyboard加载过程)
-
// 程序启动完成的时候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.创建窗口,注意窗口必须要有尺寸,尺寸跟屏幕一样大的尺寸,窗口不要被释放 appDelegate.h中有强引用的 window,所以使用self.window,以此保证创建的window不会被销毁
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor redColor];
// 2.创建窗口的跟控制器
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor yellowColor];
[vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
// 如果设置窗口的跟控制器,默认就会把控制器的view添加到窗口上
// 设置窗口的跟控制器,默认就有旋转功能
self.window.rootViewController = vc;
// [self.window addSubview:vc.view];
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
}