diff --git a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs index 42b2832..c657902 100644 --- a/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/MacCatalyst/MacOSMopups.cs @@ -1,7 +1,7 @@ using CoreGraphics; using Microsoft.Maui.Controls.Compatibility.Platform.iOS; - +using Microsoft.Maui.Platform; using Mopups.Interfaces; using Mopups.Pages; using Mopups.Platforms.MacCatalyst; @@ -36,7 +36,16 @@ public Task AddAsync(PopupPage page) if (IsiOS13OrNewer) { - var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + UIScene connectedScene = null; + + if(page.Parent != null && page.Parent.Handler != null && page.Parent.Handler.MauiContext != null) { + var nativeMainPage = page.Parent.ToPlatform(page.Parent.Handler.MauiContext); + connectedScene = nativeMainPage.Window.WindowScene; + } + + if(connectedScene == null) + connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + if (connectedScene != null && connectedScene is UIWindowScene windowScene) window = new PopupWindow(windowScene); else diff --git a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs index 7dbbe6d..3b1e48e 100644 --- a/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs +++ b/Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs @@ -28,7 +28,10 @@ public Task AddAsync(PopupPage page) if (IsiOS13OrNewer) { - var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + var connectedScene = UIApplication.SharedApplication.ConnectedScenes.ToArray() + .Where(scene => scene.Session.Role == UIWindowSceneSessionRole.Application) + .FirstOrDefault(x => x.ActivationState == UISceneActivationState.ForegroundActive); + if (connectedScene != null && connectedScene is UIWindowScene windowScene) window = new PopupWindow(windowScene); else @@ -63,6 +66,7 @@ UIWindow GetKeyWindow(UIApplication application) .ConnectedScenes .ToArray() .OfType() + .Where(scene => scene.Session.Role == UIWindowSceneSessionRole.Application) .SelectMany(scene => scene.Windows) .FirstOrDefault(window => window.IsKeyWindow);