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

后端 未结 6 1067
北海茫月
北海茫月 2020-12-31 22:33

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

相关标签:
6条回答
  • 2020-12-31 23:03

    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.

    0 讨论(0)
  • 2020-12-31 23:07

    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).

    0 讨论(0)
  • 2020-12-31 23:15

    I haven't tried this yet, but you should be able to get all the windows with [UIApplication sharedApplication].windows and then pick if you want to show the snack bar on all of the windows or one window.

    0 讨论(0)
  • 2020-12-31 23:16

    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
    }
    
    0 讨论(0)
  • 2020-12-31 23:19

    You could try:

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

    Hope this helps!

    0 讨论(0)
  • 2020-12-31 23:24

    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;
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题