Created
July 10, 2020 08:21
-
-
Save vibrazy/e0ff0e1bab0d847fefe294737407917d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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