From b9fec70743210d448200b501ce71d5116cb324a8 Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 31 May 2023 20:29:01 +0100 Subject: [PATCH] Stop using UIScreen.mainScreen to fix accent color It is deprecated and SwiftUI apps lose their accent color if it is accessed before the app's body is evaluated. --- PostHog/Internal/PHGPostHogIntegration.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/PostHog/Internal/PHGPostHogIntegration.m b/PostHog/Internal/PHGPostHogIntegration.m index e34edd1f7..8bac32e15 100644 --- a/PostHog/Internal/PHGPostHogIntegration.m +++ b/PostHog/Internal/PHGPostHogIntegration.m @@ -149,9 +149,13 @@ - (NSDictionary *)staticContext dict[@"$os_name"] = device.systemName; dict[@"$os_version"] = device.systemVersion; - CGSize screenSize = [UIScreen mainScreen].bounds.size; - dict[@"$screen_width"] = @(screenSize.width); - dict[@"$screen_height"] = @(screenSize.height); + // Access the screen via a window as UIScreen.mainScreen is deprecated + // and using it messes with the accent color in SwiftUI apps. + UIScreen *appScreen = UIApplication.sharedApplication.windows.firstObject.screen; + if (appScreen != nil) { + dict[@"$screen_width"] = @(appScreen.bounds.size.height); + dict[@"$screen_height"] = @(appScreen.bounds.size.width); + } return dict; }