Skip to content

Instantly share code, notes, and snippets.

@fredriccliver
Created April 15, 2020 20:18
Show Gist options
  • Save fredriccliver/2b2c59b8d04948f3044c8ea35dc07a06 to your computer and use it in GitHub Desktop.
Save fredriccliver/2b2c59b8d04948f3044c8ea35dc07a06 to your computer and use it in GitHub Desktop.
SceneDelegate.swift of SwiftUI project
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let contentView = ContentView().environment(\.managedObjectContext, context)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
func sceneDidDisconnect(_ scene: UIScene) {}
func sceneDidBecomeActive(_ scene: UIScene) {}
func sceneWillResignActive(_ scene: UIScene) {}
func sceneWillEnterForeground(_ scene: UIScene) {}
func sceneDidEnterBackground(_ scene: UIScene) {
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment