diff --git a/Sources/ProHUD/Alert/AlertWindow.swift b/Sources/ProHUD/Alert/AlertWindow.swift index ffcba43..ab92865 100644 --- a/Sources/ProHUD/Alert/AlertWindow.swift +++ b/Sources/ProHUD/Alert/AlertWindow.swift @@ -21,7 +21,7 @@ class AlertWindow: Window { } let w: AlertWindow if #available(iOS 13.0, *) { - if let scene = config.windowScene ?? UIApplication.shared.windows.last?.windowScene { + if let scene = config.windowScene ?? UIWindowScene.mainWindowScene { w = .init(windowScene: scene) } else { w = .init(frame: UIScreen.main.bounds) diff --git a/Sources/ProHUD/Core/Views/Window.swift b/Sources/ProHUD/Core/Views/Window.swift index 12da8c8..47991db 100644 --- a/Sources/ProHUD/Core/Views/Window.swift +++ b/Sources/ProHUD/Core/Views/Window.swift @@ -52,3 +52,15 @@ class Window: UIWindow { } } + +@available(iOS 13.0, *) +extension UIWindowScene { + + static var mainWindowScene: UIWindowScene? { + UIApplication.shared.connectedScenes.first(where: { scene in + guard let ws = scene as? UIWindowScene else { return false } + return ws.activationState == .foregroundActive + }) as? UIWindowScene + } + +} diff --git a/Sources/ProHUD/Sheet/SheetWindow.swift b/Sources/ProHUD/Sheet/SheetWindow.swift index 061400f..73ffd08 100644 --- a/Sources/ProHUD/Sheet/SheetWindow.swift +++ b/Sources/ProHUD/Sheet/SheetWindow.swift @@ -16,7 +16,7 @@ class SheetWindow: Window { init(sheet: Sheet) { self.sheet = sheet if #available(iOS 13.0, *) { - if let scene = sheet.config.windowScene ?? UIApplication.shared.windows.last?.windowScene { + if let scene = sheet.config.windowScene ?? UIWindowScene.mainWindowScene { super.init(windowScene: scene) } else { super.init(frame: UIScreen.main.bounds) diff --git a/Sources/ProHUD/Toast/ToastWindow.swift b/Sources/ProHUD/Toast/ToastWindow.swift index 04ed34c..be3a42d 100644 --- a/Sources/ProHUD/Toast/ToastWindow.swift +++ b/Sources/ProHUD/Toast/ToastWindow.swift @@ -19,7 +19,7 @@ class ToastWindow: Window { self.toast = toast super.init(frame: .zero) if #available(iOS 13.0, *) { - windowScene = toast.config.windowScene ?? UIApplication.shared.windows.last?.windowScene + windowScene = toast.config.windowScene ?? UIWindowScene.mainWindowScene } toast.window = self windowLevel = .init(rawValue: UIWindow.Level.alert.rawValue + 1000)