Skip to content

[iOS] Fix implicit engine missing didFinishLaunching and sceneWillConnect events#186558

Open
dhananjay6561 wants to merge 3 commits into
flutter:masterfrom
dhananjay6561:master
Open

[iOS] Fix implicit engine missing didFinishLaunching and sceneWillConnect events#186558
dhananjay6561 wants to merge 3 commits into
flutter:masterfrom
dhananjay6561:master

Conversation

@dhananjay6561
Copy link
Copy Markdown

Fixes #186547

Problem

When a FlutterViewController is created programmatically inside another view controller (implicit engine, non-nib path), both UISceneWillConnectNotification and application:didFinishLaunchingWithOptions: fire before the engine exists. Plugins registered via FlutterImplicitEngineDelegate.didInitializeImplicitFlutterEngine: never receive either event.

The didFinishLaunching fallback in sharedSetupWithProject:initialRoute: was gated on awokenFromNib, so it only ran for storyboard-based FlutterViewControllers. The sceneWillConnect event had no replay mechanism for this code path at all.

Fix

  • Remove the awokenFromNib guard from the didFinishLaunching fallback. sceneFallbackWillFinishLaunchingApplication: and sceneFallbackDidFinishLaunchingApplication: are already idempotent, so calling them unconditionally is safe.
  • After plugin registration, iterate UIApplication.connectedScenes and call engine:receivedConnectNotificationFor: for the first matching scene. This triggers scene:willConnectToSession:options: replay to the engine's plugins using the stored connectionOptions.

The storyboard path is unaffected: awakeFromNib runs before scene:willConnectToSession: fires, so connectedScenes is empty when the new loop runs.

Test

Updated module_uiscene_test_ios.dart to point the FlutterImplicitEngineDelegate-AppMigrated-ImplicitFlutterEngine variant at UITests-SceneEvents-ApplicationLaunchEvents.swift, which asserts that applicationWillFinishLaunchingWithOptions, applicationDidFinishLaunchingWithOptions, sceneWillConnect, sceneWillEnterForeground, and sceneDidBecomeActive are all received — matching the storyboard variant's expectations.

…nect events

When a FlutterViewController is created programmatically inside another
view controller using an implicit engine, UISceneWillConnectNotification
and application:didFinishLaunchingWithOptions: fire before the engine
exists. Plugins registered via the implicit engine callback miss both
events.

Remove the awokenFromNib guard on the didFinishLaunching fallback so it
fires for programmatic creation too (the method is idempotent). After
plugin registration, iterate already-connected scenes and call
engine:receivedConnectNotificationFor: so scene:willConnectToSession:options:
is replayed to the engine's plugins.

The storyboard (awokenFromNib) path is unaffected: awakeFromNib runs
before scene:willConnectToSession: fires, so connectedScenes is empty at
that point and the loop is a no-op.

Fixes flutter#186547
@dhananjay6561 dhananjay6561 requested a review from a team as a code owner May 15, 2026 05:14
@flutter-dashboard
Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions Bot added platform-ios iOS applications specifically engine flutter/engine related. See also e: labels. team-ios Owned by iOS platform team labels May 15, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the lifecycle event handling for implicit Flutter engines on iOS to ensure plugins correctly receive application launch and scene connection events. Specifically, it modifies FlutterViewController to forward launch events for programmatic view controllers and manually registers engines with existing scenes. A review comment identified a potential runtime crash on iOS versions earlier than 13.0 because scene-specific properties are accessed without verifying scene delegate support.

@dhananjay6561
Copy link
Copy Markdown
Author

Added FlutterSharedApplication.hasSceneDelegate guard to the new block to protect against pre-iOS 13 crashes, consistent with the pattern used above.

@dhananjay6561
Copy link
Copy Markdown
Author

Added unit tests in FlutterViewControllerTest.mm:

  • Renamed testPerformImplicitEngineCallbacksNoAppLaunchEventFallbacksWhenNoStoryboardtestPerformImplicitEngineCallbacksUsesAppLaunchEventFallbacksWhenNotAwokenFromNib and flipped the expectations to OCMVerify (programmatic implicit engines now correctly receive the fallback events).

  • New testImplicitEngineReplaysSceneWillConnectWhenNotAwokenFromNib: verifies that engine:receivedConnectNotificationFor: is called on the scene's lifecycle delegate when a programmatic FlutterViewController is created after the scene has already connected.

  • Updated testPerformImplicitEngineCallbacksNoAppLaunchEventFallbacksWhenNoScenes: changed awokenFromNib stub to NO (the condition no longer depends on it) and added a clarifying comment — the deciding factor for skipping fallbacks is the absence of connected scenes (hasSceneDelegate → NO), which is what this test verifies.

@hellohuanlin hellohuanlin added the CICD Run CI/CD label May 15, 2026
// launch events fire (storyboard-based VCs load before scene connect; programmatic VCs created
// inside another VC load even later). Plugins registered via the implicit engine callback will
// have missed didFinishLaunching. Forward it here if it hasn't been sent yet.
if (performedCallback && FlutterSharedApplication.hasSceneDelegate &&
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm, from reading the code, checking self.awokenFromNib seems to be intended so removing it may cause regression.

Can you leave this part untouched for this PR, and only fix the sceneWillConnect part?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[UIScene] Using implicit engine misses didFinishLaunching & willConnectScene events

2 participants