Skip to content

feat(ios): Add UIScene life cycle support#378

Open
Gibbo97 wants to merge 1 commit intoDev-hwang:masterfrom
Gibbo97:UIScene-life-cycle-support
Open

feat(ios): Add UIScene life cycle support#378
Gibbo97 wants to merge 1 commit intoDev-hwang:masterfrom
Gibbo97:UIScene-life-cycle-support

Conversation

@Gibbo97
Copy link
Copy Markdown

@Gibbo97 Gibbo97 commented Mar 19, 2026

feat(ios): Add UIScene lifecycle support

Closes #376.

Apple requires all UIKit apps built with the latest SDK to adopt UIScene lifecycle. In UIScene apps, applicationDidEnterBackground is never called, which prevented background app refresh from being scheduled. This adds the necessary UIScene support per the Flutter plugin migration guide.

Changes:

  • SwiftFlutterForegroundTaskPlugin now conforms to FlutterSceneLifeCycleDelegate and registers itself via registrar.addSceneDelegate(instance) so it receives scene lifecycle callbacks
  • sceneDidEnterBackground(_:) added to schedule background app refresh in UIScene apps, mirroring what applicationDidEnterBackground does for non-UIScene apps — both coexist for backward compatibility
  • FlutterForegroundTaskEarlyRegistration +load added in the Obj-C layer to register the BGTaskScheduler handler at binary load time, before Flutter initialises — required for background launch scenarios where iOS wakes the app to service a refresh task
  • isBgTaskRegistered guard added to registerAppRefresh() to prevent a double-registration crash during the UIScene transition period, when host apps may invoke GeneratedPluginRegistrant.register(with:) from both the old and new template locations
  • Flutter minimum bumped from >=3.22.0 to >=3.38.0, the version that introduced addSceneDelegate

@Gibbo97
Copy link
Copy Markdown
Author

Gibbo97 commented Mar 19, 2026

In addition, after moving to use the new flutter version this library is causing crashes when the app tries to start in the background. Ie when we have scheduled runs with workmanager, or when we try to share content into our app.

These changes resolve all this

@ppamorim
Copy link
Copy Markdown

@Dev-hwang Could you please review this PR?

ppamorim added a commit to T-Pro/flutter_foreground_task that referenced this pull request Apr 17, 2026
Apple requires UIKit apps built with the latest SDK to adopt the
UIScene lifecycle. In UIScene apps applicationDidEnterBackground is
never called, which prevented background app refresh from being
scheduled.

Changes:
- Add SceneLifecycleBridge that registers itself via an ObjC runtime
  check (responds(to:) / perform(_:with:)), so the plugin compiles on
  all Flutter versions >= 3.22.0, not only those that expose
  addSceneDelegate (introduced in Flutter 3.38.0).
- sceneDidEnterBackground schedules background app refresh, mirroring
  what applicationDidEnterBackground does for non-UIScene apps.
- Add FlutterForegroundTaskEarlyRegistration +load in the ObjC layer
  to register the BGTaskScheduler handler at binary load time, before
  Flutter initialises — required for background-launch scenarios.
- Add isBgTaskRegistered guard to registerAppRefresh() to prevent a
  double-registration crash during the UIScene transition period.

Based on Dev-hwang#378 by @Gibbo97, adapted to
avoid bumping the minimum Flutter version.
@ppamorim
Copy link
Copy Markdown

ppamorim commented Apr 17, 2026

@Gibbo97 My PR is a copy of our PR but we keep support to Flutter 3.22.0: https://github.com/T-Pro/flutter_foreground_task/pull/1

You can copy it as you want if you find it interesting.

ppamorim added a commit to T-Pro/flutter_foreground_task that referenced this pull request Apr 17, 2026
Apple requires UIKit apps built with the latest SDK to adopt the
UIScene lifecycle. In UIScene apps applicationDidEnterBackground is
never called, which prevented background app refresh from being
scheduled.

Changes:
- Add SceneLifecycleBridge that registers itself via an ObjC runtime
  check (responds(to:) / perform(_:with:)), so the plugin compiles on
  all Flutter versions >= 3.22.0, not only those that expose
  addSceneDelegate (introduced in Flutter 3.38.0).
- sceneDidEnterBackground schedules background app refresh, mirroring
  what applicationDidEnterBackground does for non-UIScene apps.
- Add FlutterForegroundTaskEarlyRegistration +load in the ObjC layer
  to register the BGTaskScheduler handler at binary load time, before
  Flutter initialises — required for background-launch scenarios.
- Add isBgTaskRegistered guard to registerAppRefresh() to prevent a
  double-registration crash during the UIScene transition period.

Based on Dev-hwang#378 by @Gibbo97, adapted to
avoid bumping the minimum Flutter version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add UIScene life cycle support

2 participants