FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: Pass NS app to the native app instead of presenting it over the… · NativeScript/NativeScript@05c2460 · GitHub

Commit 05c2460

Browse files
authored
feat: Pass NS app to the native app instead of presenting it over the root VC (#5967)
* feat: Pass NS app native controller to the native app instead of presenting it over the rootViewController When NativeScript embedded app is created from the native one we check for whether the topmost UIViewController has NativeScriptEmbedder protocol (implemented in the iOS Runtime) method 'presentNativeScriptApp:'. If yes, we call it with the NS app viewcontroller as a parameter so the embedder has control over the NS app (where and how to present it etc.) For backwards compatibility we present the NS app on top of the topmost UIViewController as a fallback. * style: Fix lint errors * feat: Check for protocol instead of selector in embedding I * Check for rootController instead of topViewController to prevent crash if !rootController * feat: Introduce NativeScriptEmbedder singleton NativeScriptEmbedder is responsive for communication between the NS and the native iOS app. His delegate will implement methods which we can call from javascript such as "presentNativeScriptApp:".
1 parent 67f9e06 commit 05c2460

9 files changed

Lines changed: 109 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ libs
55
node_modules
66
package
77
platforms
8+
!tns-core-modules/platforms
89
reports
910
tags
1011

‎tns-core-modules/application/application.ios.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,26 @@ class IOSApplication implements IOSApplicationDefinition {
131131
// TODO: Expose Window module so that it can we styled from XML & CSS
132132
this._window.backgroundColor = utils.ios.getter(UIColor, UIColor.whiteColor);
133133

134+
this.notifyAppStarted(notification);
135+
136+
}
137+
138+
public notifyAppStarted(notification?: NSNotification) {
134139
const args: LaunchEventData = {
135140
eventName: launchEvent,
136141
object: this,
137-
ios: notification.userInfo && notification.userInfo.objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") || null
142+
ios: notification && notification.userInfo && notification.userInfo.objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") || null
138143
};
139144

140145
notify(args);
141146
notify(<LoadAppCSSEventData>{ eventName: "loadAppCss", object: <any>this, cssFile: getCssFileName() });
142147

143-
this.setWindowContent(args.root);
148+
// this._window will be undefined when NS app is embedded in a native one
149+
if (this._window) {
150+
this.setWindowContent(args.root);
151+
} else {
152+
this._window = UIApplication.sharedApplication.delegate.window;
153+
}
144154
}
145155

146156
@profile
@@ -235,6 +245,7 @@ class IOSApplication implements IOSApplicationDefinition {
235245
this._window.makeKeyAndVisible();
236246
}
237247
}
248+
238249
}
239250

240251
const iosApp = new IOSApplication();
@@ -296,7 +307,14 @@ export function start(entry?: string | NavigationEntry) {
296307
if (rootController) {
297308
const controller = getViewController(rootView);
298309
rootView._setupAsRootView({});
299-
rootController.presentViewControllerAnimatedCompletion(controller, true, null);
310+
let embedderDelegate = NativeScriptEmbedder.sharedInstance().delegate;
311+
if (embedderDelegate) {
312+
embedderDelegate.performSelectorWithObject("presentNativeScriptApp:", controller);
313+
} else {
314+
let visibleVC = utils.ios.getVisibleViewController(rootController);
315+
visibleVC.presentViewControllerAnimatedCompletion(controller, true, null);
316+
}
317+
iosApp.notifyAppStarted();
300318
}
301319
}
302320
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Platform specific native code
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// NativeScriptEmbedder.h
3+
// NativeScript
4+
//
5+
// Created by Teodor Dermendzhiev on 6/19/18.
6+
//
7+
#include <UIKit/UIKit.h>
8+
9+
// When embedding NativeScript application embedder needs to conform to this protocol
10+
// in order to have control over the NativeScript UIViewController
11+
// otherwise NativeScript application is presented over the topmost UIViewController.
12+
@protocol NativeScriptEmbedderDelegate
13+
- (id)presentNativeScriptApp:(UIViewController*)vc;
14+
@end
15+
16+
@interface NativeScriptEmbedder : NSObject
17+
18+
@property(nonatomic, retain, readonly) id<NativeScriptEmbedderDelegate> delegate;
19+
20+
+ (NativeScriptEmbedder *)sharedInstance;
21+
22+
- (void)setDelegate:(id <NativeScriptEmbedderDelegate>)aDelegate;
23+
24+
25+
@end
26+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#import "NativeScriptEmbedder.h"
2+
3+
@implementation NativeScriptEmbedder
4+
5+
+ (NativeScriptEmbedder *)sharedInstance {
6+
static NativeScriptEmbedder *sharedInstance = nil;
7+
static dispatch_once_t onceToken;
8+
dispatch_once(&onceToken, ^{
9+
sharedInstance = [[NativeScriptEmbedder alloc] init];
10+
});
11+
12+
return sharedInstance;
13+
}
14+
15+
- (void)setDelegate:(id <NativeScriptEmbedderDelegate>)aDelegate {
16+
_delegate = aDelegate;
17+
}
18+
19+
@end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module NativeScriptEmbedder {
2+
header "NativeScriptEmbedder.h"
3+
export *
4+
}

‎tns-core-modules/tns-core-modules.d.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,14 @@ interface Array<T> {
160160
//Dialogs
161161
declare function alert(message?: any): void;
162162
declare function confirm(message?: string): boolean;
163+
164+
// Embedding
165+
declare interface NativeScriptEmbedderDelegate /* NSObject */ {
166+
presentNativeScriptApp(any/* UIViewController*/): any;
167+
performSelectorWithObject(string, any): any;
168+
}
169+
170+
declare class NativeScriptEmbedder {
171+
public static sharedInstance(): NativeScriptEmbedder;
172+
public delegate: NativeScriptEmbedderDelegate;
173+
}

‎tns-core-modules/utils/utils.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ export module ios {
247247
* iOS - this folder is read-only and contains the app and all its resources.
248248
*/
249249
export function getCurrentAppPath(): string;
250+
251+
/**
252+
* Gets the currently visible(topmost) UIViewController.
253+
* @param rootViewController The root UIViewController instance to start searching from (normally window.rootViewController).
254+
* Returns the visible UIViewController.
255+
*/
256+
export function getVisibleViewController(rootViewController: any/* UIViewController*/ ): any/* UIViewController*/;
250257
}
251258

252259
/**

‎tns-core-modules/utils/utils.ios.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ export module ios {
116116

117117
return NSString.stringWithString(NSString.pathWithComponents(<any>paths)).stringByStandardizingPath;
118118
}
119+
120+
export function getVisibleViewController(rootViewController: UIViewController): UIViewController {
121+
if (rootViewController.presentedViewController) {
122+
return getVisibleViewController(rootViewController.presentedViewController);
123+
}
124+
125+
if (rootViewController.isKindOfClass(UINavigationController.class())) {
126+
return getVisibleViewController((<UINavigationController>rootViewController).visibleViewController);
127+
}
128+
129+
if (rootViewController.isKindOfClass(UITabBarController.class())) {
130+
let selectedTab = (<UITabBarController>rootViewController).selectedViewController;
131+
return getVisibleViewController(<UITabBarController>rootViewController);
132+
}
133+
134+
return rootViewController;
135+
136+
}
137+
119138
}
120139

121140
export function GC() {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL