-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get current window screen size (#247)
* feat: get current window screen size * fix: wrap in compiler directive * fix: lint * fix: build for tvOS and watchOS * fix: filter foregrounded scenes by default * feat: search for UIWindowSceneDelegate when getting current window * feat: get device size for watchOS * fix: iterate over window scenes once * fix: iOS15 fallback * chore: update CHANGELOG
- Loading branch information
Showing
8 changed files
with
80 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// UIApplication+.swift | ||
// PostHog | ||
// | ||
// Created by Yiannis Josephides on 11/11/2024. | ||
// | ||
|
||
#if os(iOS) || os(tvOS) | ||
import UIKit | ||
|
||
extension UIApplication { | ||
static func getCurrentWindow(filterForegrounded: Bool = true) -> UIWindow? { | ||
let windowScenes = UIApplication.shared | ||
.connectedScenes | ||
.compactMap { $0 as? UIWindowScene } | ||
.filter { | ||
!filterForegrounded || $0.activationState == .foregroundActive | ||
} | ||
|
||
for scene in windowScenes { | ||
// attempt to retrieve directly from UIWindowScene | ||
if #available(iOS 15.0, tvOS 15.0, *) { | ||
if let keyWindow = scene.keyWindow { | ||
return keyWindow | ||
} | ||
} else { | ||
// check scene.windows.isKeyWindow | ||
for window in scene.windows { | ||
if window.isKeyWindow { | ||
return window | ||
} | ||
} | ||
} | ||
|
||
// check scene.delegate.window property | ||
let sceneDelegate = scene.delegate as? UIWindowSceneDelegate | ||
if let target = sceneDelegate, let window = target.window { | ||
return window | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
#endif |