用户关闭应用程序后,如何以及在何处执行功能?

Den*_*ver 0 swift

如果用户关闭应用程序,我可以包含运行一次的函数吗?如果是这样,我应该把那个函数放在哪里,因为我不知道用户将从哪个视图控制器关闭应用程序。解决办法是什么?

Adr*_*ian 5

AppDelegate.swift

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
Run Code Online (Sandbox Code Playgroud)