IOS AppDelegate

ios程序启动顺序

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        print("application:构造函数")
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        print("applicationWillResignActive:程序即将取消激活状态")
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        print("applicationDidEnterBackground:程序进入后台")
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        print("applicationWillEnterForeground:程序即将进入前台")
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        print("applicationDidBecomeActive:程序被激活");
        view?.removeFromSuperview()
    }

    func applicationWillTerminate(_ application: UIApplication) {
        print("applicationWillTerminate:程序关闭");
    }

·启动程序时顺序:构造函数->程序被激活


程序启动.png

·进入切换程序界面(双击home键)执行:程序即将取消激活状态


切换界面.png

·从切换界面返回程序执行:程序被激活
·返回桌面(单击home键)执行顺序:程序即将取消激活状态->程序进入后台
返回桌面.png

·从桌面回到程序:程序即将进入前台->程序被激活
*例:如果想让app在进程切换界面不显示程序内容的话,可以在applicationWillResignActive的时候放一张图在覆盖window,然后在applicationDidBecomeActive的时候去掉

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        print("application:构造函数")
        view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        view?.backgroundColor = UIColor.red
        return true
    }
func applicationWillResignActive(_ application: UIApplication) {
        print("applicationWillResignActive:程序即将取消激活状态")
        self.window?.addSubview(view!)
        self.window?.bringSubview(toFront: view!)
    }
func applicationDidBecomeActive(_ application: UIApplication) {
        print("applicationDidBecomeActive:程序被激活");
        view?.removeFromSuperview()
    }
切换.gif

你可能感兴趣的:(IOS AppDelegate)