Created
February 2, 2023 02:45
-
-
Save iHTCboy/ab5be69a5122c07a85dbe3cee362c83a to your computer and use it in GitHub Desktop.
iOS get Current KeyWindow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIApplication { | |
public var currentKeyWindow: UIWindow? { | |
if #available(iOS 13.0, *) { | |
if let window = connectedScenes | |
.filter({ $0.activationState == .foregroundActive }) | |
.map({ $0 as? UIWindowScene }) | |
.compactMap({ $0 }) | |
.first?.windows | |
.filter({ $0.isKeyWindow }).first { | |
return window | |
} | |
} | |
if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first { | |
return window | |
} else { | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment