Ok. I figured it out. This might be helpful to others.
Step 1 Add this to AppDelegate
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
if connectingSceneSession.role == .windowApplication {
configuration.delegateClass = SceneDelegate.self
}
return configuration
}
}
Step 2 Create SceneDelegate
class SceneDelegate: NSObject, ObservableObject, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
self.window = windowScene.keyWindow
}
}
Step 3. Then call it like this....
@EnvironmentObject var sceneDelegate: SceneDelegate
.
.
.
.
if let myWindow = sceneDelegate.window {
print(myWindow.description)
guard let scene = myWindow.windowScene else { return }
let options = UIWindowSceneDestructionRequestOptions()
options.windowDismissalAnimation = .commit
UIApplication.shared.requestSceneSessionDestruction(scene.session, options: options, errorHandler: { error in
// TODO: - Handle error
print("Error")
})
}