Skip to content

Commit

Permalink
fix: wrap in compiler directive
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj committed Nov 11, 2024
1 parent 4ffb8f1 commit 93c471a
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions PostHog/Utils/UIApplication+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
// Created by Yiannis Josephides on 11/11/2024.
//

import UIKit
#if canImport(UIKit)
import UIKit

extension UIApplication {
static func getCurrentWindow(checkForegrounded: Bool = false) -> UIWindow? {
let windowScenes = UIApplication.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter {
!checkForegrounded || $0.activationState == .foregroundActive
}
extension UIApplication {
static func getCurrentWindow(checkForegrounded: Bool = false) -> UIWindow? {
let windowScenes = UIApplication.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter {
!checkForegrounded || $0.activationState == .foregroundActive
}

if #available(iOS 15.0, *) {
// attempt to retrieve directly from UIWindowScene
if let keyWindow = windowScenes.compactMap(\.keyWindow).first {
return keyWindow
if #available(iOS 15.0, *) {
// attempt to retrieve directly from UIWindowScene
if let keyWindow = windowScenes.compactMap(\.keyWindow).first {
return keyWindow
}
}
}

// fall back to iterating for `isKeyWindow`
return windowScenes
.flatMap(\.windows)
.first(where: { $0.isKeyWindow })
// fall back to iterating for `isKeyWindow`
return windowScenes
.flatMap(\.windows)
.first(where: { $0.isKeyWindow })
}
}
}

#endif

0 comments on commit 93c471a

Please sign in to comment.