USB-Meter / USB Meter / SceneDelegate.swift
Newer Older
74 lines | 3.473kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  SceneDelegate.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 01/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import UIKit
10
import SwiftUI
11

            
12
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
13

            
14
    var window: UIWindow?
15

            
16

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

            
19
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
20
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
21
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
22

            
23
        // Get the managed object context from the shared persistent container.
24
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
25

            
26
        // Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
27
        // Add `@Environment(\.managedObjectContext)` in the views that will need the context.
28
        let contentView = ContentView()
29
            .environment(\.managedObjectContext, context)
30
            .environmentObject(appData)
31

            
32
        // Use a UIHostingController as window root view controller.
33
        if let windowScene = scene as? UIWindowScene {
34
            let window = UIWindow(windowScene: windowScene)
35
            window.rootViewController = UIHostingController(rootView: contentView)
36
            self.window = window
37
            window.makeKeyAndVisible()
38
        }
39
    }
40

            
41
    func sceneDidDisconnect(_ scene: UIScene) {
42
        // Called as the scene is being released by the system.
43
        // This occurs shortly after the scene enters the background, or when its session is discarded.
44
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
45
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
46
    }
47

            
48
    func sceneDidBecomeActive(_ scene: UIScene) {
49
        // Called when the scene has moved from an inactive state to an active state.
50
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
51
    }
52

            
53
    func sceneWillResignActive(_ scene: UIScene) {
54
        // Called when the scene will move from an active state to an inactive state.
55
        // This may occur due to temporary interruptions (ex. an incoming phone call).
56
    }
57

            
58
    func sceneWillEnterForeground(_ scene: UIScene) {
59
        // Called as the scene transitions from the background to the foreground.
60
        // Use this method to undo the changes made on entering the background.
61
    }
62

            
63
    func sceneDidEnterBackground(_ scene: UIScene) {
64
        // Called as the scene transitions from the foreground to the background.
65
        // Use this method to save data, release shared resources, and store enough scene-specific state information
66
        // to restore the scene back to its current state.
67

            
68
        // Save changes in the application's managed object context when the application transitions to the background.
69
        (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
70
    }
71

            
72

            
73
}
74