From eab903e9e0633ddd5cbbfd31df6207c20fed48f9 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 11 Oct 2016 10:10:35 +0300 Subject: [PATCH] Existing iOS integration app support added --- tns-core-modules/application/application.d.ts | 6 ++ .../application/application.ios.ts | 67 ++++++++++++------- tns-core-modules/ui/page/page.ios.ts | 6 +- 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/tns-core-modules/application/application.d.ts b/tns-core-modules/application/application.d.ts index 2291bc608..8da56067e 100644 --- a/tns-core-modules/application/application.d.ts +++ b/tns-core-modules/application/application.d.ts @@ -559,6 +559,12 @@ declare module "application" { */ rootController: any /* UIViewController */; + /* tslint:enable */ + /** + * The key window. + */ + window: any /* UIWindow */; + /** * The [UIApplication](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html). */ diff --git a/tns-core-modules/application/application.ios.ts b/tns-core-modules/application/application.ios.ts index be73a178e..c8da2cf4c 100644 --- a/tns-core-modules/application/application.ios.ts +++ b/tns-core-modules/application/application.ios.ts @@ -81,6 +81,10 @@ class IOSApplication implements definition.iOSApplication { return utils.ios.getter(UIApplication, UIApplication.sharedApplication); } + get window(): Window { + return this._window; + } + get delegate(): typeof UIApplicationDelegate { return this._delegate; } @@ -121,27 +125,7 @@ class IOSApplication implements definition.iOSApplication { typedExports.notify(args); - let rootView = args.root; - let frame: Frame; - let navParam: Object; - if (!rootView) { - // try to navigate to the mainEntry/Module (if specified) - navParam = typedExports.mainEntry; - if (!navParam) { - navParam = typedExports.mainModule; - } - - if (navParam) { - frame = new Frame(); - frame.navigate(navParam); - } else { - // TODO: Throw an exception? - throw new Error("A Frame must be used to navigate to a Page."); - } - - rootView = frame; - } - + let rootView = createRootView(args.root); this._window.content = rootView; if (rootView instanceof Frame) { @@ -225,7 +209,6 @@ class IOSApplication implements definition.iOSApplication { }); } } - } var iosApp = new IOSApplication(); @@ -259,6 +242,31 @@ export function addCss(cssText: string) { } } +function createRootView(v?) { + let rootView = v; + let frame: Frame; + let navParam: Object; + if (!rootView) { + // try to navigate to the mainEntry/Module (if specified) + navParam = typedExports.mainEntry; + if (!navParam) { + navParam = typedExports.mainModule; + } + + if (navParam) { + frame = new Frame(); + frame.navigate(navParam); + } else { + // TODO: Throw an exception? + throw new Error("A Frame must be used to navigate to a Page."); + } + + rootView = frame; + } + + return rootView; +} + var started: boolean = false; typedExports.start = function (entry?: NavigationEntry) { if (!started) { @@ -267,7 +275,20 @@ typedExports.start = function (entry?: NavigationEntry) { } started = true; loadCss(); - UIApplicationMain(0, null, null, typedExports.ios && typedExports.ios.delegate ? NSStringFromClass(typedExports.ios.delegate) : NSStringFromClass(Responder)); + + if(!iosApp.nativeApp) { + // Normal NativeScript app will need UIApplicationMain. + UIApplicationMain(0, null, null, typedExports.ios && typedExports.ios.delegate ? NSStringFromClass(typedExports.ios.delegate) : NSStringFromClass(Responder)); + } else { + let rootView = createRootView(); + if(rootView) { + // Attach to the existing iOS app + let rootController = iosApp.nativeApp.keyWindow.rootViewController; + rootController.presentViewControllerAnimatedCompletion(rootView.ios.controller, utils.ios.MajorVersion >= 7, null); + uiUtils.ios._layoutRootView(rootView, utils.ios.getter(UIScreen, UIScreen.mainScreen).bounds); + } + } + } else { throw new Error("iOS Application already started!"); } diff --git a/tns-core-modules/ui/page/page.ios.ts b/tns-core-modules/ui/page/page.ios.ts index 0f1603c24..7f1175f22 100644 --- a/tns-core-modules/ui/page/page.ios.ts +++ b/tns-core-modules/ui/page/page.ios.ts @@ -1,4 +1,5 @@ -import pageCommon = require("./page-common"); +import application = require("application"); +import pageCommon = require("./page-common"); import { View } from "ui/core/view"; import trace = require("trace"); import uiUtils = require("ui/utils"); @@ -130,6 +131,9 @@ class UIViewControllerImpl extends UIViewController { } } else { + if(!application.ios.window) { + uiUtils.ios._layoutRootView(owner, utils.ios.getter(UIScreen, UIScreen.mainScreen).bounds); + } owner._updateLayout(); } }