Skip to content

Instantly share code, notes, and snippets.

@iHTCboy
Created February 2, 2023 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iHTCboy/ab5be69a5122c07a85dbe3cee362c83a to your computer and use it in GitHub Desktop.
Save iHTCboy/ab5be69a5122c07a85dbe3cee362c83a to your computer and use it in GitHub Desktop.
iOS get Current KeyWindow
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