0

The question is similar to this one:

Display a view or splash screen before applicationDidEnterBackground (to avoid active view screenshot)

The link above also shows code examples how to implement it.

I need to show for example white screen when the app enters background on "multitasking screen": enter image description here

The problem is that way doesn't work on iOS 13! How to fix this issue?

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49

2 Answers2

1

You show that in this UIWindowSceneDelegate method. Implement whatever logic you want in it.

func sceneDidEnterBackground(_ scene: UIScene)
Suh Fangmbeng
  • 573
  • 4
  • 16
1

I use this (my own) implementation for functionality you ask for both iOS 12 & iOS 13 support

AppDelegate:

private var blankWindow: BlankWindow?

// MARK: Shared AppDelegate

extension AppDelegate {
    static func blankWindowShouldAppear(blankWindow: inout BlankWindow?) {
        blankWindow = BlankWindow(frame: UIScreen.main.bounds)
        blankWindow?.makeKeyAndVisible()
    }

    static func blankWindowShouldDisappear(window: UIWindow?, blankWindow: inout BlankWindow?) {
        window?.makeKeyAndVisible()
        blankWindow = nil
    }

    @available(iOS 13.0, *)
    static func blankWindowShouldAppear(_ windowScene: UIWindowScene, blankWindow: inout BlankWindow?) {
        blankWindow = BlankWindow(windowScene: windowScene)
        blankWindow?.makeKeyAndVisible()
    }
}

// MARK: Old life cycle methods

extension AppDelegate {
    /// ⚠️ Methods here will not be called under iOS 13 due to new SceneDelegate life cycle

    func applicationWillEnterBackground(_ application: UIApplication) {
        AppDelegate.blankWindowShouldAppear(blankWindow: &blankWindow)
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        AppDelegate.blankWindowShouldDisappear(window: window, blankWindow: &blankWindow)
    }
}

SceneDelegate:

private var blankWindow: BlankWindow?

// MARK: New life cycle methods

@available(iOS 13.0, *)
extension SceneDelegate {
    /// ⚠️ As for now, we use fallback to AppDelegate shared methods to reduce code duplication
    /// Not all of the new life cycle methods are implemented here, yet

    func sceneWillEnterForeground(_ scene: UIScene) {
        AppDelegate.blankWindowShouldDisappear(window: window, blankWindow: &blankWindow)
    }

    func sceneWillEnterBackground(_ scene: UIScene) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        AppDelegate.blankWindowShouldAppear(windowScene, blankWindow: &blankWindow)
    }
}

Where BlankWindow class is a UIWindow you wanna show to a user at this moment

kikiwora
  • 1,724
  • 11
  • 7
  • would be nice if you could give the BlankWIndow implementation – Async- Mar 05 '21 at 13:39
  • @Async- the BlankWindow may be any subclass of UIWindow, you may customize it however you want. There is now special or universal implementation for that :) – kikiwora Mar 06 '21 at 14:14