Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 47fa4a4

Browse files
committed
Added the ability to set properties in interface builder for FlutterViewController.
1 parent cfa3d90 commit 47fa4a4

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

shell/platform/darwin/ios/framework/Headers/FlutterViewController.h

+19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ FLUTTER_EXPORT
7878
nibName:(nullable NSString*)nibName
7979
bundle:(nullable NSBundle*)nibBundle NS_DESIGNATED_INITIALIZER;
8080

81+
/**
82+
* Initializer that is called from loading a FlutterViewController from a XIB.
83+
*
84+
* See also:
85+
* https://developer.apple.com/documentation/foundation/nscoding/1416145-initwithcoder?language=objc
86+
*/
87+
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER;
88+
8189
/**
8290
* Registers a callback that will be invoked when the Flutter view has been rendered.
8391
* The callback will be fired only once.
@@ -194,6 +202,17 @@ FLUTTER_EXPORT
194202
*/
195203
@property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;
196204

205+
/**
206+
* If the `FlutterViewController` creates a `FlutterEngine`, this property
207+
* determines if that `FlutterEngine` has `allowHeadlessExecution` set.
208+
*
209+
* The intention is that this is used with the XIB. Otherwise, a
210+
* `FlutterEngine` can just be sent to the init methods.
211+
*
212+
* See also: `-[FlutterEngine initWithName:project:allowHeadlessExecution:]`
213+
*/
214+
@property(nonatomic, readonly) BOOL engineAllowHeadlessExecution;
215+
197216
@end
198217

199218
NS_ASSUME_NONNULL_END

shell/platform/darwin/ios/framework/Source/FlutterViewController.mm

+24-12
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,26 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
111111
return self;
112112
}
113113

114+
- (void)sharedSetupWithProject:(nullable FlutterDartProject*)project {
115+
_viewOpaque = YES;
116+
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterViewController>>(self);
117+
_engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter"
118+
project:project
119+
allowHeadlessExecution:self.engineAllowHeadlessExecution]);
120+
_flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]);
121+
[_engine.get() createShell:nil libraryURI:nil];
122+
_engineNeedsLaunch = YES;
123+
_ongoingTouches = [[NSMutableSet alloc] init];
124+
[self loadDefaultSplashScreenView];
125+
[self performCommonViewControllerInitialization];
126+
}
127+
114128
- (instancetype)initWithProject:(nullable FlutterDartProject*)project
115129
nibName:(nullable NSString*)nibName
116130
bundle:(nullable NSBundle*)nibBundle {
117131
self = [super initWithNibName:nibName bundle:nibBundle];
118132
if (self) {
119-
_viewOpaque = YES;
120-
_weakFactory = std::make_unique<fml::WeakPtrFactory<FlutterViewController>>(self);
121-
_engine.reset([[FlutterEngine alloc] initWithName:@"io.flutter"
122-
project:project
123-
allowHeadlessExecution:NO]);
124-
_flutterView.reset([[FlutterView alloc] initWithDelegate:_engine opaque:self.isViewOpaque]);
125-
[_engine.get() createShell:nil libraryURI:nil];
126-
_engineNeedsLaunch = YES;
127-
_ongoingTouches = [[NSMutableSet alloc] init];
128-
[self loadDefaultSplashScreenView];
129-
[self performCommonViewControllerInitialization];
133+
[self sharedSetupWithProject:project];
130134
}
131135

132136
return self;
@@ -137,7 +141,15 @@ - (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBun
137141
}
138142

139143
- (instancetype)initWithCoder:(NSCoder*)aDecoder {
140-
return [self initWithProject:nil nibName:nil bundle:nil];
144+
self = [super initWithCoder:aDecoder];
145+
return self;
146+
}
147+
148+
- (void)awakeFromNib {
149+
[super awakeFromNib];
150+
if (!_engine.get()) {
151+
[self sharedSetupWithProject:nil];
152+
}
141153
}
142154

143155
- (instancetype)init {

0 commit comments

Comments
 (0)