Hosting Controller When Using iOS 14 @main
Here is a possible approach (tested with Xcode 12 / iOS 14)... but if you intend to use UIKit features heavily it is better to use UIKit Life-Cycle, as it gives more flexibility to configure UIKit part.
struct ContentView: View {
var body: some View {
Text("Demo Root Controller access")
.withHostingWindow { window in
if let controller = window?.rootViewController {
// .. do something with root view controller
}
}
}
}
extension View {
func withHostingWindow(_ callback: @escaping (UIWindow?) -> Void) -> some View {
self.background(HostingWindowFinder(callback: callback))
}
}
struct HostingWindowFinder: UIViewRepresentable {
var callback: (UIWindow?) -> ()
func makeUIView(context: Context) -> UIView {
let view = UIView()
DispatchQueue.main.async { [weak view] in
self.callback(view?.window)
}
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
I was facing the same problem. I played around with an alternative solution with zero set up, meaning it would work with SwiftUI App and Playgrounds (I even wrote a set of Playgrounds for documentation) - The package is called SwiftUIWindowBinder.
Example using WindowBinder
... See docs for other usage, such as event view modifiers (like onTapGesture
), or the convenience of WindowButton
.
import SwiftUI
import SwiftUIWindowBinder
struct ContentView : View {
/// Host window state (will be bound)
@State var window: Window?
var body: some View {
// Create a WindowBinder and bind it to the state property `window`
WindowBinder(window: $window) {
Text("Hello")
.padding()
.onTapGesture {
guard let window = window else {
return
}
print(window.description)
}
}
}
}
Only caveat of the package is you cannot use a host window to construct your view. I have a whole Playground page on this.