Читать книгу SwiftUI For Dummies - Wei-Meng Lee - Страница 20

SceneDelegate.swift

Оглавление

Whereas the AppDelegate.swift file is responsible for handling your app life cycle, the SceneDelegate.swift file is responsible for your scene's life cycle.

The SceneDelegate.swift file contains the following default functions:

 scene(_:willConnectTo:options:)

 sceneDidDisconnect(_:)

 sceneDidBecomeActive(_:)

 sceneWillResignActive(_:)

 sceneWillEnterForeground(_:)

 sceneDidEnterBackground(_:)

The scene(_:willConnectTo:options:) function is called when a scene is added to the app (in simple terms, when your UI is shown). Here, you load the content of the file named ContentView (which is what you've modified earlier in the ContentView.swift file):

func scene(_ scene: UIScene, willConnectTo session:

UISceneSession, options connectionOptions:

UIScene.ConnectionOptions) {

let contentView = ContentView()

if let windowScene = scene as? UIWindowScene {

let window = UIWindow(windowScene:

windowScene)

window.rootViewController =

UIHostingController(rootView: contentView)

self.window = window

window.makeKeyAndVisible()

}

}

In short, you use AppDelegate.swift to perform setup needed for the duration of the app. You also use it to handle events that focus on the app, as well as registered for external services like push notifications. The SceneDelegate.swift, on the other hand, is designed to handle events for multi-window OS (iPadOS), which supports multiple instances of your app’s UI.

SwiftUI For Dummies

Подняться наверх