iOS之UIApplication详解(2)
1 // 2 3 // AppDelegate.m 4 5 // AppDelegate 6 7 // 8 9 // Created by apple on 13-7-10. 10 11 // Copyright (c) 2013年 itcast. All rights reserved. 12 13 // 14 15 16 17 #import "AppDelegate.h" 18 19 20 21 #import "ViewController.h" 22 23 24 25 @implementation AppDelegate 26 27 28 29 - (void)dealloc 30 31 { 32 33 [_window release]; 34 35 [_viewController release]; 36 37 [super dealloc]; 38 39 } 40 41 42 43 #pragma mark app加载完毕(默认情况下,只会打印1次) 44 45 // launchOptions : 包含应用启动的一些信息(比如通过点击通知打开应用) 46 47 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 48 49 { 50 51 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 52 53 // Override point for customization after application launch. 54 55 self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; 56 57 self.window.rootViewController = self.viewController; 58 59 [self.window makeKeyAndVisible]; 60 61 62 63 NSLog(@"didFinishLaunchingWithOptions--app加载完毕"); 64 65 return YES; 66 67 } 68 69 70 71 #pragma mark 失去焦点 72 73 // 程序进入Inactive状态 74 75 - (void)applicationWillResignActive:(UIApplication *)application 76 77 { 78 79 NSLog(@"applicationWillResignActive-未激活"); 80 81 } 82 83 84 85 #pragma mark 进入后台 86 87 // 一般在这个方法中保存数据 88 89 - (void)applicationDidEnterBackground:(UIApplication *)application 90 91 { 92 93 NSLog(@"applicationDidEnterBackground-进入后台"); 94 95 96 97 // 让程序保持后台运行状态 98 99 [application beginBackgroundTaskWithExpirationHandler:^{ 100 101 NSLog(@"后台运行时间到期了"); 102 103 }]; 104 105 106 107 // 取消所有的通知 108 109 [application cancelAllLocalNotifications]; 110 111 112 113 UILocalNotification *note = [[[UILocalNotification alloc] init] autorelease]; 114 115 // 设置通知的文字 116 117 note.alertBody = @"赶紧回来吧,哥!"; 118 119 // 设置通知出现的时间间隔 120 121 note.repeatInterval = NSMinuteCalendarUnit; 122 123 // 设置通知出现的声音 124 125 // note.soundName = @"xxx.wav"; 126 127 // 设置通知第一次出现的时间 128 129 note.fireDate = [NSDate dateWithTimeIntervalSinceNow:4]; 130 131 132 133 // 发布通知 134 135 // [application presentLocalNotificationNow:<#(UILocalNotification *)#>]; 136 137 [application scheduleLocalNotification:note]; 138 139 } 140 141 142 143 #pragma mark 进入前台 144 145 - (void)applicationWillEnterForeground:(UIApplication *)application 146 147 { 148 149 NSLog(@"applicationWillEnterForeground-进入前台"); 150 151 } 152 153 154 155 #pragma mark 当app被激活(获得了焦点,可以跟用户进行交互) 156 157 // 这时候程序处于:Running状态 158 159 - (void)applicationDidBecomeActive:(UIApplication *)application 160 161 { 162 163 NSLog(@"applicationDidBecomeActive-正在运行"); 164 165 } 166 167 168 169 #pragma mark 当app接收到内存警告 170 171 // 一般在这里回收不必要内存 172 173 // 1.很容易就创建回来的对象 174 175 // 2.占用内存很大的对象 176 177 - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { 178 179 NSLog(@"applicationDidReceiveMemoryWarning-内存警告"); 180 181 } 182 183 184 185 186 187 #pragma mark 当程序处于后天运行状态,并且程序被关闭了就会调用 188 189 - (void)applicationWillTerminate:(UIApplication *)application 190 191 { 192 193 NSLog(@"applicationWillTerminate--程序被关闭"); 194 195 } 196 197 @end 198 199