From 9345347587c48f7ae1303130eddcbb4748b705ad Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 9 Jul 2020 17:23:32 -0700 Subject: [PATCH] Added the ability to set properties in interface builder for FlutterViewController. (#19458) --- .../framework/Headers/FlutterViewController.h | 19 ++++++++++ .../framework/Source/FlutterViewController.mm | 36 ++++++++++++------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h b/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h index 20c59326756df..6f434af1047f7 100644 --- a/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h +++ b/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h @@ -78,6 +78,14 @@ FLUTTER_EXPORT nibName:(nullable NSString*)nibName bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER; +/** + * Initializer that is called from loading a FlutterViewController from a XIB. + * + * See also: + * https://developer.apple.com/documentation/foundation/nscoding/1416145-initwithcoder?language=objc + */ +- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER; + /** * Registers a callback that will be invoked when the Flutter view has been rendered. * The callback will be fired only once. @@ -194,6 +202,17 @@ FLUTTER_EXPORT */ @property(nonatomic, readonly) NSObject* binaryMessenger; +/** + * If the `FlutterViewController` creates a `FlutterEngine`, this property + * determines if that `FlutterEngine` has `allowHeadlessExecution` set. + * + * The intention is that this is used with the XIB. Otherwise, a + * `FlutterEngine` can just be sent to the init methods. + * + * See also: `-[FlutterEngine initWithName:project:allowHeadlessExecution:]` + */ +@property(nonatomic, readonly) BOOL engineAllowHeadlessExecution; + @end NS_ASSUME_NONNULL_END diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 24079b436d6b9..d788b461586f6 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -111,22 +111,26 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine return self; } +- (void)sharedSetupWithProject:(nullable FlutterDartProject*)project { + _viewOpaque = YES; + _weakFactory = std::make_unique>(self); + _engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter" + project:project + allowHeadlessExecution:self.engineAllowHeadlessExecution]); + _flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]); + [_engine.get() createShell:nil libraryURI:nil]; + _engineNeedsLaunch = YES; + _ongoingTouches = [[NSMutableSet alloc] init]; + [self loadDefaultSplashScreenView]; + [self performCommonViewControllerInitialization]; +} + - (instancetype)initWithProject:(nullable FlutterDartProject*)project nibName:(nullable NSString*)nibName bundle:(nullable NSBundle*)nibBundle { self = [super initWithNibName:nibName bundle:nibBundle]; if (self) { - _viewOpaque = YES; - _weakFactory = std::make_unique>(self); - _engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter" - project:project - allowHeadlessExecution:NO]); - _flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]); - [_engine.get() createShell:nil libraryURI:nil]; - _engineNeedsLaunch = YES; - _ongoingTouches = [[NSMutableSet alloc] init]; - [self loadDefaultSplashScreenView]; - [self performCommonViewControllerInitialization]; + [self sharedSetupWithProject:project]; } return self; @@ -137,7 +141,15 @@ - (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBun } - (instancetype)initWithCoder:(NSCoder*)aDecoder { - return [self initWithProject:nil nibName:nil bundle:nil]; + self = [super initWithCoder:aDecoder]; + return self; +} + +- (void)awakeFromNib { + [super awakeFromNib]; + if (!_engine.get()) { + [self sharedSetupWithProject:nil]; + } } - (instancetype)init {