//
// AppDelegate.m
// UI1_应用的程序的生命周期
//
// Created by zhangxueming on 15/6/29.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
//
#import "AppDelegate.h"
//开发工具:
//1.xcode : 编辑工程代码, 修改工程
//2.IOS simulator : 模拟器,在mac电脑上模拟iphone 设备的运行环境
//3.instrument : 内存分析工具
//4.iphone开发工具包(SDK): 苹果官方提供的开发环境
//5.interface bulider(xib): 提供UI界面与用户的接口
//代码创建
//沙盒(sandbox)
//出于安全考虑,每一个应用程序都有自己独立的文件目录结构
//应用程序之间不能共享数据
//应用程序的状态
//Not running 未运行 程序没启动
//Inactive 未激活 程序在前台运行,不过没有接收到事件。在没有事件处理情况下程序通常停留在这个状态
//Active 激活 程序在前台运行而且接收到了事件。这也是前台的一个正常的模式
//Backgroud 后台 程序在后台而且能执行代码,大多数程序进入这个状态后会在在这个状态上停留一会。时间到之后会进入挂起状态(Suspended)。有的程序经过特殊的请求后可以长期处于Backgroud状态
//Suspended 挂起 程序在后台不能执行代码。系统会自动把程序变成这个状态而且不会发出通知。当挂起时,程序还是停留在内存中的,当系统内存低时,系统就把挂起的程序清除掉,为前台程序提供更多的内存。
@interface AppDelegate ()
@end
@implementation AppDelegate
//应用程序启动调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//write code here
self.window.backgroundColor = [UIColor yellowColor];
self.window.rootViewController = nil;
//获取当前应用程序对象
UIApplication *app = [UIApplication sharedApplication];
//获取当前应用程序的代理
AppDelegate *delegateApp = app.delegate;
NSLog(@"app = %@ delegate = %@", app, delegateApp);
return YES;
}
//将要进入后台模式(锁屏)的时候
- (void)applicationWillResignActive:(UIApplication *)application {
// 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.
//1.外部中断事件进入,如: 电话 或者短消息接入
//2.从前台切换后台,及退出应用程序
//3.暂停正在进行的任务,暂停定时器,降低OpenGL 帧率, 暂停正在运行的游戏
NSLog(@"%@ has been called", NSStringFromSelector(_cmd));
}
//进入后台后调用该方法
- (void)applicationDidEnterBackground:(UIApplication *)application {
// 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.
//释放共享资源,保存用户数据,作废定时器,存储足够的应用程序状态信息目的从后台恢复应用程序到前台
//如果应用程序支持后台模式,该方法替代applicationWillTerminate:
NSLog(@"%@ has been called!!!", NSStringFromSelector(_cmd));
}
//应用程序将要进入前台时调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
// 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.
//启动定时器,提高OpenGL帧率 启动游戏
NSLog(@"%@ has been called!!!", NSStringFromSelector(_cmd));
}
//进入到前台模式后
- (void)applicationDidBecomeActive:(UIApplication *)application {
// 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.
//重新启动被暂停的任务, 刷新用户界面
NSLog(@"%@ has been called!!!", NSStringFromSelector(_cmd));
}
//内存告急被调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
//释放内存
NSLog(@"%@ has been called!!!", NSStringFromSelector(_cmd));
}
//应用程序停止被调用(应用程序不支持后台模式)
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
NSLog(@"%@ has been called!!!", NSStringFromSelector(_cmd));
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "-000phone.com.UI1___________" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"UI1___________" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"UI1___________.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
@end
转载于:https://www.cnblogs.com/0515offer/p/4638183.html