USB-Meter / USB Meter / SceneDelegate.swift
Newer Older
75 lines | 3.507kb
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) {
Bogdan Timofte authored 2 weeks ago
18

            
Bogdan Timofte authored 2 weeks ago
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).
Bogdan Timofte authored 2 weeks ago
22

            
Bogdan Timofte authored 2 weeks ago
23
        // Get the managed object context from the shared persistent container.
24
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Bogdan Timofte authored 2 weeks ago
25
        appData.activateCloudDeviceSync(context: context)
Bogdan Timofte authored 2 weeks ago
26

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

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

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

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

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

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

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

            
Bogdan Timofte authored 2 weeks ago
69
        // Save changes in the application's managed object context when the application transitions to the background.
70
        (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
71
    }
72

            
73

            
74
}
75