Swift基础Swift - UI

2、App生命周期及AppDelegate、SceneDeleg

2020-03-02  本文已影响0人  爱玩游戏的iOS菜鸟

iOS12及以前

关于App的生命周期中的各个方法
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    
    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        
        return true
    }
    
    //应用程序即将被挂起时执行
    func applicationWillResignActive(_ application: UIApplication) {
        log("app即将被挂起")
    }
    
    //应用程序入活动状态时(已经被激活)时执行
    func applicationDidBecomeActive(_ application: UIApplication) {
        log("app已经被激活")
    }
    
    //程序进入后台的执行
    func applicationDidEnterBackground(_ application: UIApplication) {
        log("app已经进入后台")
    }
    
    //应用程序即将回到前台
    func applicationWillEnterForeground(_ application: UIApplication) {
        log("app即将回到前台")
    }

    //当程序将要退出是被调用,一般用来数据的保存和退出前的整理工作
    func applicationWillTerminate(_ application: UIApplication) {
        log("app即将被杀死")
    }
APP常见的操作相应的方法调用顺序
  1. 启动app
func application(_ : , willFinishLaunchingWithOptions:)
func application(_ : , didFinishLaunchingWithOptions:)
func applicationDidBecomeActive(_ : ) 
func application(_ : , didReceiveRemoteNotification:)//接受处理消息通知
  1. 双击home键或屏幕上划使app上移挂起
func applicationWillResignActive(_ :)
  1. 再次双击home键归位或点击回到该应用
func applicationDidBecomeActive(_ :)
  1. 单击home键使app进入后台
func applicationWillResignActive(_ :)
func applicationDidEnterBackground(_ :)
  1. 打开后台运行的app
func applicationWillEnterForeground(_ :)
func applicationDidBecomeActive(_ :)
  1. 点击通知
func applicationWillEnterForeground(_ :)
func application(_ : , didReceiveRemoteNotification:)//接受处理消息通知
func applicationDidBecomeActive(_ :)
  1. 杀死App进程
func applicationWillTerminate(_ :)
  1. app切换到其他应用之后,再切换回来
func applicationWillEnterForeground(_ :)
func application(_ : , open url: , options: )// 应用间传值 支付或文件传输
func applicationDidBecomeActive(_ :)

iOS13

func scene(_:willConnectTo:options:)
func sceneDidDisconnect(_:)//场景进入后台,但未丢弃
func sceneDidBecomeActive(_:)//场景已回到前台,重新启动已暂停或新开始的任务
//applicationWillTerminate 应用被杀死
func sceneWillResignActive(_:)//场景即将进入后台
func sceneWillEnterForeground(_:)//场景即将进入前台
func sceneDidEnterBackground(_:)//场景已经进入前台

把视图层级相关的方法从 didFinishLanchingWithOptions 迁移到 SceneDelegate 中的 willConnectTo 方法中,有两个好处:

  1. 首先可以提高预览结果呈现的速度
  2. 在应用进入后台时,不会做一些额外的操作,只会做一些必要的操作,使应用快速进入睡眠状态,以节省耗电
上一篇下一篇

猜你喜欢

热点阅读