From 5ce1afaeda4b78c6ef4fa4151cc69b2966a2e3f3 Mon Sep 17 00:00:00 2001 From: Themis wang Date: Thu, 7 Nov 2024 14:44:05 -0500 Subject: [PATCH 1/2] Change deprecated keyWindow to new API --- .../UIKit/FPRUIViewControllerInstrument.m | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m b/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m index 147b53c516a..ab5bbfbf99e 100644 --- a/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m +++ b/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m @@ -69,12 +69,33 @@ void InstrumentViewDidAppear(FPRUIViewControllerInstrument *instrument, // This has to be called on the main thread and so it's done here instead of in // FPRScreenTraceTracker. - // TODO(#13067): Replace keyWindow usage (deprecated in iOS and unavailable in visionOS). #if !defined(TARGET_OS_VISION) || !TARGET_OS_VISION -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - if ([((UIViewController *)_self).view isDescendantOfView:FPRSharedApplication().keyWindow]) { -#pragma clang diagnostic pop + NSArray *windows = nil; + + if (@available(iOS 13.0, *)) { + NSArray *scenes = FPRSharedApplication().connectedScenes.allObjects; + for (UIScreen *scene in scenes) { + if ([scene isKindOfClass:[UIWindowScene class]]) { + windows = [(UIWindowScene *)scene windows]; + break; + } + } + } else { + windows = FPRSharedApplication().windows; + } + + if (!windows || windows.count == 0) { + return; + } + + UIWindow *foundKeyWindow = nil; + for (UIWindow *window in windows) { + if (window.isKeyWindow) { + foundKeyWindow = window; + break; + } + } + if (foundKeyWindow && [((UIViewController *)_self).view isDescendantOfView:foundKeyWindow]) { [[FPRScreenTraceTracker sharedInstance] viewControllerDidAppear:_self]; } #endif From 3845f729b959daf122d7883ce1c9ddb299b151e4 Mon Sep 17 00:00:00 2001 From: Themis wang Date: Thu, 7 Nov 2024 19:22:25 -0500 Subject: [PATCH 2/2] find a even better api --- .../UIKit/FPRUIViewControllerInstrument.m | 29 +------------------ 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m b/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m index ab5bbfbf99e..1c2226148ca 100644 --- a/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m +++ b/FirebasePerformance/Sources/Instrumentation/UIKit/FPRUIViewControllerInstrument.m @@ -69,36 +69,9 @@ void InstrumentViewDidAppear(FPRUIViewControllerInstrument *instrument, // This has to be called on the main thread and so it's done here instead of in // FPRScreenTraceTracker. -#if !defined(TARGET_OS_VISION) || !TARGET_OS_VISION - NSArray *windows = nil; - - if (@available(iOS 13.0, *)) { - NSArray *scenes = FPRSharedApplication().connectedScenes.allObjects; - for (UIScreen *scene in scenes) { - if ([scene isKindOfClass:[UIWindowScene class]]) { - windows = [(UIWindowScene *)scene windows]; - break; - } - } - } else { - windows = FPRSharedApplication().windows; - } - - if (!windows || windows.count == 0) { - return; - } - - UIWindow *foundKeyWindow = nil; - for (UIWindow *window in windows) { - if (window.isKeyWindow) { - foundKeyWindow = window; - break; - } - } - if (foundKeyWindow && [((UIViewController *)_self).view isDescendantOfView:foundKeyWindow]) { + if (FPRSharedApplication() && ((UIViewController *)_self).view.window.keyWindow) { [[FPRScreenTraceTracker sharedInstance] viewControllerDidAppear:_self]; } -#endif }]; }