首页 > 解决方案 > 当应用程序状态暂停时,是否有任何功能可以查找应用程序何时从多任务栏中被杀死?

问题描述

当用户在应用程序保持在后台大约 3 到 4 小时后向上滑动从应用程序切换器中删除应用程序时,我想清除应用程序数据。

是否有任何功能或委托可以知道应用程序何时被杀死而没有进入前台。

我希望在用户从应用切换器中终止应用时打开登录页面。否则,它应该打开主页。

标签: iosswift

解决方案


斯威夫特的解决方案:

在您的 AppDelegate.swift 文件中添加以下函数(如果不存在):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // clean your apps data here
    cleanAppData()
    //show the login
    showLogin()
}

仅当应用程序“冷启动”时才调用此函数。

这表示:

  • 如果应用程序第一次启动,则调用它
  • 如果应用程序被 iOS“杀死”
  • 如果应用程序被用户“杀死”
  • 如果应用程序在后台运行并再次打开,则不会调用它

然后将以下函数(如果不存在)添加到 AppDelegate.swift:

func applicationWillEnterForeground(_ application: UIApplication) {
    // 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.
    // show the homepage
    showHomePage()
}

仅当应用程序“热启动”时才调用此函数,这意味着如果应用程序之前运行并由用户发送到后台。


推荐阅读