For the More Curious: Scene State Transitions

Logging the scene state transition delegate methods is a good way to get a better understanding of the various scene state transitions. You can make these changes in the same copy of the project that you used for the last section or in your main LootLogger project; they do not conflict with code in later chapters.

In SceneDelegate.swift, implement four of these methods so that they print out their names. If the template created these methods for you, update them as shown in Listing 13.15. If not, you will need to add them.

Rather than hardcoding the name of the method in the call to print(), use the #function expression. At compile time, the #function expression will evaluate to a String representing the name of the method.

Listing 13.15  Implementing scene state transition delegate methods (SceneDelegate.swift)

func sceneWillResignActive(_ scene: UIScene) {
    print(#function)
}

func sceneDidEnterBackground(_ scene: UIScene) {
    print(#function)
}

func sceneWillEnterForeground(_ scene: UIScene) {
    print(#function)
}

func sceneDidBecomeActive(_ scene: UIScene) {
    print(#function)
}

Add the same print() statement to the top of scene(_:willConnectTo:options:).

Listing 13.16  Printing scene(_:willConnectTo:options:) (SceneDelegate.swift)

func scene(_ scene: UIScene,
           willConnectTo session: UISceneSession,
           options connectionOptions: UIScene.ConnectionOptions) {

    print(#function)
    guard let _ = (scene as? UIWindowScene) else { return }
    ...
}

Build and run the application. You will see that the scene gets sent scene(_:willConnectTo:options:) and then sceneDidBecomeActive(_:). Play around to see what actions cause what transitions.

Switch to the Home screen, and the console will report that the scene briefly inactivated and then went into the background state. Relaunch the application by tapping its icon on the Home screen. The console will report that the scene entered the foreground and then became active.

Press Command-Shift-H twice to open the task switcher. Swipe the LootLogger application up and off this display to quit the application. Note that no method is called on your scene delegate at this point – it is simply terminated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.10.246