Currently, when migrating to UIScene, you add the FlutterImplicitEngineDelegate to your AppDelegate and add a didInitializeImplicitFlutterEngine callback. This callback is called when the FlutterEngine is created by the FlutterViewController, primarily from a storyboard.
- @objc class AppDelegate: FlutterAppDelegate {
+ @objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
- GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
+ func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
+ GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
+ }
}
|
BOOL performedCallback = [_engine performImplicitEngineCallback]; |
This change was made due the Storyboard initializing later in the lifecycle than before. It now initializes after application:didFinishLaunchingWithOptions:.
However, due to this, plugins cannot receive application:didFinishLaunchingWithOptions: event because the engine is not created yet.
To work around this, we call those methods once the engine is created by the storyboard
|
// When migrated to scenes, the FlutterViewController from the storyboard is initialized after the |
|
// application launch events. Therefore, plugins may not be registered yet since they're expected |
|
// to be registered during the implicit engine callbacks. As a workaround, send the app launch |
|
// events after the application callbacks. |
|
if (self.awokenFromNib && performedCallback && FlutterSharedApplication.hasSceneDelegate && |
|
[appDelegate isKindOfClass:[FlutterAppDelegate class]]) { |
|
id applicationLifeCycleDelegate = ((FlutterAppDelegate*)appDelegate).lifeCycleDelegate; |
|
[applicationLifeCycleDelegate |
|
sceneFallbackWillFinishLaunchingApplication:FlutterSharedApplication.application]; |
|
[applicationLifeCycleDelegate |
|
sceneFallbackDidFinishLaunchingApplication:FlutterSharedApplication.application]; |
|
} |
However, this seem to be problematic:
- BGTaskScheduler requires
Registration of all launch handlers must be complete before the end of [applicationDidFinishLaunching(_:)
May also be related: #183900
When first investigating UIScene, FlutterLaunchEngine was introduced. This allowed plugins to still be registered in application:didFinishLaunchingWithOptions: by creating a FlutterEngine and saving it to the FlutterAppDelegate, which can be accessed via the singleton app instance. Then once the FlutterViewController initializes, it will “take” the FlutterEngine from the FlutterAppDelegate instead of creating one.
There was some disagreement on the API, though, which is why we changed to this.
https://flutter.dev/go/ios-ui-scene-lifecycle-migration
go/flutter-ios-uiscene-delegate-adoption-v1
I wonder if there's some way we could still use the FlutterLaunchEngine but still with the FlutterImplicitEngineDelegate
Currently, when migrating to UIScene, you add the
FlutterImplicitEngineDelegateto your AppDelegate and add adidInitializeImplicitFlutterEnginecallback. This callback is called when theFlutterEngineis created by theFlutterViewController, primarily from a storyboard.flutter/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm
Line 308 in 72cfdc7
This change was made due the Storyboard initializing later in the lifecycle than before. It now initializes after
application:didFinishLaunchingWithOptions:.However, due to this, plugins cannot receive
application:didFinishLaunchingWithOptions:event because the engine is not created yet.To work around this, we call those methods once the engine is created by the storyboard
flutter/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm
Lines 318 to 329 in 72cfdc7
However, this seem to be problematic:
Registration of all launch handlers must be complete before the end of [applicationDidFinishLaunching(_:)May also be related: #183900
When first investigating UIScene,
FlutterLaunchEnginewas introduced. This allowed plugins to still be registered inapplication:didFinishLaunchingWithOptions:by creating aFlutterEngineand saving it to theFlutterAppDelegate, which can be accessed via the singleton app instance. Then once theFlutterViewControllerinitializes, it will “take” theFlutterEnginefrom theFlutterAppDelegateinstead of creating one.There was some disagreement on the API, though, which is why we changed to this.
https://flutter.dev/go/ios-ui-scene-lifecycle-migration
go/flutter-ios-uiscene-delegate-adoption-v1
I wonder if there's some way we could still use the
FlutterLaunchEnginebut still with theFlutterImplicitEngineDelegate