当应用程序进入后台时显示 window 涵盖所有内容

Show window that covers everything when apps enters background

在 iOS12 及以下我曾经使用类似的东西在所有内容之上显示一个 window 来覆盖我的应用程序内容。这曾经有效,但在 iOS13 测试版中,这不再有效。

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var coverWindow: UIWindow?

    func applicationDidEnterBackground(_ application: UIApplication) {
        if self.coverWindow != nil {
            // Skip since cover window is already showing
            return
        }
        let vc = UIViewController()
        let label = UILabel(frame: window!.bounds)
        label.text = "CoverWindow. Tap to app see contents"
        vc.view = label
        vc.view.backgroundColor = UIColor.lightGray
        let coverWindow = UIWindow(frame: window!.bounds)
        coverWindow.rootViewController = vc
        coverWindow.windowLevel = .alert
        coverWindow.makeKeyAndVisible()
        self.coverWindow = coverWindow
    }

}

显然 window 在应用程序再次进入前台之前,更改不会反映在屏幕上。

问题

有谁知道如何修复或解决这个问题?或者这种方法不正确?

任何帮助将不胜感激

备注

  1. 我不使用简单视图,因为我的应用程序可能也会显示其他 windows,而我的要求是涵盖所有内容。

  2. 我不使用applicationWillResignActive因为我们只想在进入后台时显示coverWindow。 (TouchID 身份验证和其他内容可能会触发 applicationWillResignActive 并且 coverWindow 会错误显示)

示例代码

下载Full working example code in Github(运行在iOS模拟器12和13中查看区别)

你必须实现应用程序生命周期,你只需删除它,添加那些应用程序生命周期功能并实现你的代码,它将运行没有错误

给自己的答案。

我已将此报告给 Apple,它已在 iOS 13.1 左右修复。 iOS13 的最新版本没有这个错误:)