[Xcode10 实际操作]一、博主领进门-(8)应用代理文件(AppDelegate.swift)详解

本文将演示使用iOS模拟器,演示程序的生命周期。

在项目导航区,打开应用代理文件【AppDelegate.swift】

应用代理文件时系统运行本应用的委托,里面定义了如程序的进入与退出、设备方向旋转等众多全局方法。

 1 import UIKit
 2 
 3 @UIApplicationMain
 4 class AppDelegate: UIResponder, UIApplicationDelegate {
 5 
 6     var window: UIWindow?
 7 
 8     //把程序载入后需要执行的代码,写在程序完成加载的方法里面,这是最常用的一个方法
 9     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
10         // Override point for customization after application launch.
11         
12         //当程序完成加载的过程后,在控制台输出一行文字
13         print(">>>>>>>>>>>>>>>>>>>>>>>> didFinishLaunchingWithOptions")
14         return true
15     }
16 
17     //当程序将要进入非活动状态时,调用此方法,在此期间,程序不会接受消息或事件
18     func applicationWillResignActive(_ application: UIApplication) {
19         // 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.
20         // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
21         print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillResignActive")
22     }
23 
24     //当程序被推送到后台的时候,调用此方法
25     //如果要设置后台继续某些动作,则在这个方法里面添加代码即可
26     func applicationDidEnterBackground(_ application: UIApplication) {
27         // 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.
28         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
29         print(">>>>>>>>>>>>>>>>>>>>>>>> applicationDidEnterBackground")
30     }
31 
32     //当程序从后台,将要重新回到前台的时候,调用此方法
33     func applicationWillEnterForeground(_ application: UIApplication) {
34         // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
35         print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillEnterForeground")
36     }
37 
38     //当程序进入活动状态的时候,执行该方法
39     func applicationDidBecomeActive(_ application: UIApplication) {
40         // 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.
41         print(">>>>>>>>>>>>>>>>>>>>>>>> applicationDidBecomeActive")
42     }
43 
44     //当程序将要退出时,调用该方法
45     //通常是用来保存数据,和一些退出前的清理工作
46     func applicationWillTerminate(_ application: UIApplication) {
47         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48         print(">>>>>>>>>>>>>>>>>>>>>>>> applicationWillTerminate")
49     }
50 }

【Hardware】硬件->【Home】->返回模拟器的主界面。

【Hardware】硬件->【Lock】->锁定模拟器。

解锁模拟器:

方式一:双击【Home】

方式二:【Command】+【Shift】+两下【H】

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10117588.html