ios - 当应用程序状态暂停时,是否有任何功能可以从多任务栏中查找应用程序何时被杀死?

标签 ios swift

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

是否有任何函数或委托(delegate)可以知道应用程序何时在不进入前台的情况下被杀死。

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

最佳答案

Swift 的解决方案:

在您的 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()
}

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

关于ios - 当应用程序状态暂停时,是否有任何功能可以从多任务栏中查找应用程序何时被杀死?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56254814/

相关文章:

ios - UICollectionViewCell : expand to full size 内的 UITableView

iOS 应用程序因内存限制而被杀死

arrays - Swift 中 MemoryLayout.size 中数组的大小

jquery mobile - 在 iPhone 中加载长 ListView 时页面闪烁

ios - 带有选择器错误的快速覆盖方法

ios - 在 iOS 中设置 tableView 内的 editText 长度限制

ios - systemLayoutSizeFittingSize 没有为我的 UILabel 返回足够大的尺寸

swift - 使用 Moya 将参数附加到每个网络调用

swift - 以编程方式更改 UIImage 位置

ios - TableView Swift 中的多项选择