From 29004d9048ab84d774d6978e685ea7b0e47b625b Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Thu, 19 May 2022 15:11:11 -0700 Subject: [PATCH] feat(ios): Utils.getRootViewController --- packages/core/utils/native-helper.d.ts | 5 +++++ packages/core/utils/native-helper.ios.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/core/utils/native-helper.d.ts b/packages/core/utils/native-helper.d.ts index 8fed91420..e21f9f131 100644 --- a/packages/core/utils/native-helper.d.ts +++ b/packages/core/utils/native-helper.d.ts @@ -132,6 +132,11 @@ export namespace iOSNativeHelper { export function nsArrayToJSArray(a: NSArray): T[]; } + /** + * Get the root UIViewController of the app + */ + export function getRootViewController(): any; /* UIViewController */ + /** * Data serialize and deserialize helpers */ diff --git a/packages/core/utils/native-helper.ios.ts b/packages/core/utils/native-helper.ios.ts index d01d85532..b6d70fc87 100644 --- a/packages/core/utils/native-helper.ios.ts +++ b/packages/core/utils/native-helper.ios.ts @@ -136,6 +136,16 @@ export namespace iOSNativeHelper { } } + export function getRootViewController(): UIViewController { + const app = UIApplication.sharedApplication; + const win = app.keyWindow || (app.windows && app.windows.count > 0 && app.windows.objectAtIndex(0)); + let vc = win.rootViewController; + while (vc && vc.presentedViewController) { + vc = vc.presentedViewController; + } + return vc; + } + export function isLandscape(): boolean { console.log('utils.ios.isLandscape() is deprecated; use application.orientation instead');