How get current keywindow equivalent for multi window SceneDelegate Xcode 11?

uiscenesession
uiwindow ios 13
appdelegate window is nil
swiftui get uiwindow
get current window ios swift
uiwindowscene
use the statusbarmanager object on the window scene instead
ios 13 rootviewcontroller

I'm converting my iOS13 app for iPadOS to SceneDelegate (multi window).

How can I get the current UIWindow from the current SceneDelegate?

I know that a can access the current scene using UIView.window or UIViewController.view.window, but I have a non UI class (AppDelegate) where I need to get the window (keyWindow until iOS12) to show a snack bar on top of everything.

I used to do [UIApplication sharedApplication].keyWindow but now, of course, that's wrong.


You'll want to simply iterate through all your windows and find the key window manually.

for (UIWindow *window in [UIApplication sharedApplication].windows) {
    if (window.isKeyWindow) {
        // you have the key window
        break;
    }
}

DO NOT use UISceneActivationStateForegroundActive as the check. That means something different and folks are introducing bugs by using logic to find the first UISceneActivationStateForegroundActive window scene.

Opening a popup of windows in iOS 13 in Xcode 11.3 |Apple , You'll want to simply iterate through all your windows and find the key window manually. for (UIWindow *window in [UIApplication sharedApplication].windows)​  How can I get the current UIWindow from the current SceneDelegate? I know that a can access the current scene using UIView.window or UIViewController.view.window, but I have a non UI class (AppDelegate) where I need to get the window (keyWindow until iOS12) to show a snack bar on top of everything.


Swift 5.x / iOS 13.x

Inspired by many solutions, to get back my old window property (searching it inside the connectedScenes) I've realized a little extension:

extension UIApplication {
    var currentWindow: UIWindow? {
        connectedScenes
        .filter({$0.activationState == .foregroundActive})
        .map({$0 as? UIWindowScene})
        .compactMap({$0})
        .first?.windows
        .filter({$0.isKeyWindow}).first
    }
}

Usage:

if let window = UIApplication.shared.currentWindow { 
   // do whatever you want with window
}

Scene Delegate In Xcode 11 & iOS 13 – LearnAppMaking, After upgrading Xcode to version 11.3, popup windows calling in iOS 13 stopped working because keyWindow is deprecated, the code is UIViewController. https://stackoverflow.com/questions/57009283/how-get-current-keywindow-​equivalent-for-multi-window-scenedelegate-xcode-11. This helped me  'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes. So if you are not supporting multiple windows on iPad there is no objection to going ahead and continuing to use keyWindow.


It sounds like you probably want to move this logic to your SceneDelegate.

The SceneDelegate now has knowledge of whether the window is connected to the scene, so it might make sense to have every connected scene listen to whatever event is driving the snack bar, and the show it on its window. This would then result in every visible window showing the snack bar (1 or more).

How to work with SceneDelegate in iOS 12, Since Xcode 11, the scene delegate is added automatically with the default iOS app project template. In essence, the window is the app, and most iOS apps only have one window. Get started with iOS 13 and Swift 5 Especially the concept of having one app with multiple scenes is interesting,  [ Natty] xcode How get current keywindow equivalent for multi window SceneDelegate Xcode 11? By: Georges Samaha 1.0 ; [ Natty ] sql-server Connection to a MS SQL database from Google scripts By: terpimost 0.5 ;


You could try:

    if let window = UIApplication.shared.windows.first(where: { (window) -> Bool in window.isKeyWindow}) {
    //your code
    }

Hope this helps!

Understanding the Essentials for Adopting Multiwindow Support on , For information about how to configure scene support, see Specifying the Scenes Your App Supports. keyWindow. Show most recent active  If you started your project in Xcode 11 and you've checked the "Supports multiple windows" checkbox, you've done the first couple of steps to support multiple scenes. Your app can now have more than one active scene, and iOS will always use your SceneDelegate to set up your scene.


Now you have more than one window, one for each scene. First, you have to answer which one you need at the moment of usage.

Probably you want to get the window of the currently active scene then you can use this:

    UIWindow* window = nil;
    if (@available(iOS 13.0, *))
    {
        for (UIWindowScene* wScene in [UIApplication sharedApplication].connectedScenes)
        {
            if (wScene.activationState == UISceneActivationStateForegroundActive)
            {
                window = wScene.windows.firstObject;

                break;
            }
        }
    }

How get current keywindow equivalent for multi window SceneDelegate Xcode 11?. I'm converting my iOS13 app for iPadOS to SceneDelegate (multi window). To make use of iOS 13’s scene-based app lifecycle we need to be using at least Xcode 11. At the time of writing, the current released version of Xcode is 11.3. Open Xcode and select File->New


Amongst those features, iPadOS adds Multiwindow support, which allows our apps There is also the equivalent of Mission Control for currently open apps. you will see all the Windows you have open for both the current app, and other apps. This is what the App Delegate looks like in Xcode 11 GM 1:. I have a problem getting the current UIScene when multiple scenes are connected.. Specifically, my app (iOS 13) supports multiple windows on the iPad. When I have two windows side by side and I access the apps connected scenes (with UIApplication.shared.connectedScenes), all of them have the activationState set to .foregroundActive.


When you create a new project in Xcode 11, you might notice something that you haven’t seen before. Instead of only creating an AppDelegate.swift file, a ViewController.swift, a storyboard and some other files, Xcode now creates a new file for you; the SceneDelegate.swift file.


New SceneDelegate to manage multiple windows at the same time XCTUnwrap is equivalent to: but also for experienced ones to get to know the new UI of Xcode 11;