Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SwiftUI screen size handling on iOS #238

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions PostHog/PostHogContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ class PostHogContext {
var properties: [String: Any] = [:]

#if os(iOS) || os(tvOS)
properties["$screen_width"] = Float(UIScreen.main.bounds.width)
properties["$screen_height"] = Float(UIScreen.main.bounds.height)
if let screen = UIApplication.shared.windows.first?.screen {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means that all iOS/tvOS consumers receive a warning unless we're only calling into PostHog from the main thread. If a client's logging system runs off of main, accessing UIApplication.shared will cause warnings in Xcode. Not sure if that's a priority for y'all, but figured I'd raise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @dfed, yeah that is indeed true. Hopping and blocking to main thread for every single event is not realistic so we should probably promote these to static properties. NSScreen.main will probably need to stay as dynamic context since it represents the screen of the currently focused window which can change, but I think for iOS/tvOS it's probably safe to calculate this once. Let me think about it a bit and I'll get back soon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can check if the captured event is on the main thread and only do the thread jumping if needed to reduce the perf impact as well.
UIApplication.shared.windows is also deprecated so the change from A to B didn't make sense IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHogReplayIntegration.getCurrentWindow() is likely the correct way right now, we should move getCurrentWindow() out of PostHogReplayIntegration and reuse across the SDK, I see more places using UIApplication.shared that is just trying to find the active window, eg UIViewController.activeController and UIApplication.ph_currentWindow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHogReplayIntegration.getCurrentWindow() is likely the correct way right now, we should move getCurrentWindow() out of PostHogReplayIntegration and reuse across the SDK, I see more places using UIApplication.shared that is just trying to find the active window, eg UIViewController.activeController and UIApplication.ph_currentWindow

Agreed. For capturing events we probably want to skip scene state checks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

properties["$screen_width"] = Float(screen.bounds.width)
properties["$screen_height"] = Float(screen.bounds.height)
}
#elseif os(macOS)
if let mainScreen = NSScreen.main {
let screenFrame = mainScreen.visibleFrame
Expand Down
Loading