From fcca0a980279a81ff6ad74ca979ce1d9e1ee1ba2 Mon Sep 17 00:00:00 2001 From: Manuel Beck Date: Fri, 10 Oct 2025 16:51:23 +0200 Subject: [PATCH 1/3] iOS: Update top view controller retrieval for iOS multi-window - Refactored getTopPresentedViewController to use the active UIWindowScene for keyWindow selection, improving compatibility with iOS multi-window environments. - Using `[UIApplication sharedApplication].keyWindow` is deprecated since iOS 13 --- src/ios/CDVNotification.m | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ios/CDVNotification.m b/src/ios/CDVNotification.m index bea6466b..f66f750a 100644 --- a/src/ios/CDVNotification.m +++ b/src/ios/CDVNotification.m @@ -149,14 +149,26 @@ - (void)beep:(CDVInvokedUrlCommand*)command } -(UIViewController *)getTopPresentedViewController { + UIWindow *keyWindow = nil; + + // Get the first active window scene + for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) { + if (windowScene.activationState == UISceneActivationStateForegroundActive) { + keyWindow = windowScene.windows.firstObject; + break; + } + } + UIViewController *presentingViewController = self.viewController; - if (presentingViewController.view.window != [UIApplication sharedApplication].keyWindow){ - presentingViewController = [UIApplication sharedApplication].keyWindow.rootViewController; + + if (presentingViewController.view.window != keyWindow) { + presentingViewController = keyWindow.rootViewController; } while (presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed]){ presentingViewController = presentingViewController.presentedViewController; } + return presentingViewController; } From 989d93000725fc7a81f0bc759995526683339448 Mon Sep 17 00:00:00 2001 From: Manuel Beck Date: Sat, 11 Oct 2025 17:27:58 +0200 Subject: [PATCH 2/3] feat(ios): update getTopPresentedViewController for support iOS 11 and 13 --- src/ios/CDVNotification.m | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/ios/CDVNotification.m b/src/ios/CDVNotification.m index f66f750a..138a82e6 100644 --- a/src/ios/CDVNotification.m +++ b/src/ios/CDVNotification.m @@ -151,12 +151,25 @@ - (void)beep:(CDVInvokedUrlCommand*)command -(UIViewController *)getTopPresentedViewController { UIWindow *keyWindow = nil; - // Get the first active window scene - for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) { - if (windowScene.activationState == UISceneActivationStateForegroundActive) { - keyWindow = windowScene.windows.firstObject; - break; + if (@available(iOS 13.0, *)) { + // iOS 13+ approach - get the first active window scene + // Since iOS 13, Apple introduced UIScene and multiple window support. + // The deprecated keyWindow property doesn't work reliably with multiple scenes + // as it returns a key window across all connected scenes, which can be from + // different app instances or windows. We need to find the active foreground + // scene to get the correct window for presenting our alert. + for (UIWindowScene *windowScene in [UIApplication sharedApplication].connectedScenes) { + if (windowScene.activationState == UISceneActivationStateForegroundActive) { + keyWindow = windowScene.windows.firstObject; + break; + } } + } else { + // Fallback for iOS 11-12 + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + keyWindow = [UIApplication sharedApplication].keyWindow; + #pragma clang diagnostic pop } UIViewController *presentingViewController = self.viewController; From 7009688fd4b54927353a43877d360d8a496600c7 Mon Sep 17 00:00:00 2001 From: Manuel Beck Date: Sun, 12 Oct 2025 12:29:01 +0200 Subject: [PATCH 3/3] CVDNotification.m: More generic comment for using deprecated keyWindow usage on older iOS - This plugin currently supports OSs older than iOS 11 --- src/ios/CDVNotification.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/CDVNotification.m b/src/ios/CDVNotification.m index 138a82e6..89bd626f 100644 --- a/src/ios/CDVNotification.m +++ b/src/ios/CDVNotification.m @@ -165,7 +165,7 @@ -(UIViewController *)getTopPresentedViewController { } } } else { - // Fallback for iOS 11-12 + // Fallback for older iOS versions #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" keyWindow = [UIApplication sharedApplication].keyWindow;