Skip to content

Instantly share code, notes, and snippets.

@vibrazy
Created July 10, 2020 08:21
Show Gist options
  • Save vibrazy/e0ff0e1bab0d847fefe294737407917d to your computer and use it in GitHub Desktop.
Save vibrazy/e0ff0e1bab0d847fefe294737407917d to your computer and use it in GitHub Desktop.
//
// Created by Dan Tavares on 10/07/2020.
//
import SwiftUI
struct WindowKey: EnvironmentKey {
static let defaultValue: UIWindow? = {
guard
let firstScene = UIApplication.shared.connectedScenes.first,
let windowScene = firstScene as? UIWindowScene
else {
return nil
}
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIViewController()
return window
}()
}
extension EnvironmentValues {
var rootWindow: UIWindow? {
get {
return self[WindowKey.self]
}
set {
self[WindowKey.self] = newValue
}
}
}
struct MyAwesomeApp: App {
@Environment(\.rootWindow) private var rootWindow
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// Make sure you call
// rootWindow?.makeKeyAndVisible()
// Then you can use rootWindow?.rootViewController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment