SwiftUI For Dummies. Wei-Meng Lee

Чтение книги онлайн.

Читать онлайн книгу SwiftUI For Dummies - Wei-Meng Lee страница 13

SwiftUI For Dummies - Wei-Meng Lee

Скачать книгу

static var previews: some View {

       Group {

       ContentView()

       ContentView()

       .previewDevice(PreviewDevice(

       rawValue: "iPhone SE"))

       .previewDisplayName("iPhone SE")

       }

       }

       }

      Now that you've seen how to get started with SwiftUI, let’s take a moment to examine the various files created in the project and see how the various parts connect.

       AppDelegate.swift

       SceneDelegate.swift

       ContentView.swift (this is the file that you've been modifying to create the UI of your iOS application)

       Info.plist

Screenshot of the HelloSwiftUI page displaying the following files: AppDelegate.swift; SceneDelegate.swift; ContentView.swift; and Info.plist.

      FIGURE 1-17: The content of the project created.

      Info.plist

      Within the Application Scene Manifest key, you have the following keys:

       Enable Multiple Windows: This is set to NO by default. You can set this to YES if you're building apps for iPadOS and macOS.

       Application Session Role: An array that contains a list of dictionary objects. The default object contains a key named Delegate Class Name that points to the SceneDelegate.swift file.

Screenshot of the HelloSwiftUI page for examining the items in the Info.plist file displaying the Application Scene Manifest key that contains a list of dictionary objects.

      FIGURE 1-18: Examining the items in the Info.plist file.

      AppDelegate.swift

      AppDelegate.swift is the place where you write code to handle an application's launch, going into the background, coming to the foreground, and other activities.

      AppDelegate.swift has three main functions:

       application(:didFinishLaunchingWithOptions) -> Bool: This function is called when the application is launched. You can use this function to perform your setup for the app when it's launched.

       application(: configurationForConnecting:options:) -> UISceneConfiguration: This function is called whenever your app is needed to supply a new scene. Here, it returns the default item in the dictionary named Default Configuration: func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { // Called when a new scene session is being //created. Use this method to select a //configuration to create the new scene with. return UISceneConfiguration( name: "Default Configuration", sessionRole: connectingSceneSession.role)} A scene is an object that represents one instance of your app's user interface.

       application(:didDiscardSceneSessions:): This function is called whenever a user discards a scene (such as swiping it away in the multitasking window).

      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(_:)

       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.

Скачать книгу