Home Swift warning: 'windows' was deprecated in iOS 15.0
Post
Cancel

Swift warning: 'windows' was deprecated in iOS 15.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import SwiftUI

extension UIApplication {
    var keyWindow: UIWindow? {
        // Get connected scenes
        return UIApplication.shared.connectedScenes
        // Keep only active scenes, onscreen and visible to the user
            .filter { $0.activationState == .foregroundActive }
        // Keep only the first `UIWindowScene`
            .first(where: { $0 is UIWindowScene })
        // Get its associated windows
            .flatMap({ $0 as? UIWindowScene })?.windows
        // Finally, keep only the key window
            .first(where: \.isKeyWindow)
    }
}

/*
    From:
        https://stackoverflow.com/a/68989580
    Usage e.g.:
        let windowScene = UIApplication.shared.keyWindow?.windowScene
 */
This post is licensed under CC BY 4.0 by the author.