Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created March 22, 2020 18:52
Show Gist options
  • Save vialyx/edd2f7513b9a3eb0b7a83e4da1ee07c8 to your computer and use it in GitHub Desktop.
Save vialyx/edd2f7513b9a3eb0b7a83e4da1ee07c8 to your computer and use it in GitHub Desktop.
....
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Get the managed object context from the shared persistent container.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
// Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
// Add `@Environment(\.managedObjectContext)` in the views that will need the context.
let contentView = ContentView().environment(\.managedObjectContext, context)
let viewModel = ViewModel()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(viewModel))
self.window = window
window.makeKeyAndVisible()
}
}
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment