Skip to content

Commit ec31a4e

Browse files
committed
feat(apple): let the host app supply the FlutterDartProject for background engines
BackgroundWorker created its headless FlutterEngine with project: nil, so every task built a fresh FlutterDartProject whose settings can differ from the main engine's. Add WorkmanagerPlugin.setDartProject(_:) so the host app can share one project with the background engines. Behavior is unchanged when it is not called.
1 parent 896c4c1 commit ec31a4e

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎build.log‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Warning: Building for device with codesigning disabled. You will have to manually codesign before deploying to device.
2+
Building dev.fluttercommunity.workmanagerExample for device (ios)...
3+
All plugins found for ios are Swift Packages, but your project still has CocoaPods integration. Your project uses a non-standard Podfile and will need to be migrated to Swift Package Manager manually. Some steps you may need to complete include:
4+
* In the ios/ directory run "pod deintegrate"
5+
* Transition any Pod dependencies to Swift Package equivalents. See https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app
6+
* Transition any custom logic
7+
* Remove the include to "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" in your ios/Flutter/Debug.xcconfig
8+
* Remove the include to "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" in your ios/Flutter/Release.xcconfig
9+
10+
Removing CocoaPods integration will improve the project's build time.
11+
Running pod install... 520ms
12+
Running Xcode build...
13+
Xcode build done. 15,1s
14+
✓ Built build/ios/iphoneos/Runner.app

‎docs/quickstart.mdx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ import workmanager_apple
8585
}
8686
```
8787

88+
Background engines create their own `FlutterDartProject` by default. If your
89+
app needs them to use the same project as the main engine (for example when a
90+
code push tool resolves the newest downloaded patch each time a project is
91+
created), create one project at launch, pass it to your engine, and call
92+
`WorkmanagerPlugin.setDartProject(_:)` with it before any task runs:
93+
94+
```swift
95+
let project = FlutterDartProject()
96+
WorkmanagerPlugin.setDartProject(project)
97+
```
98+
99+
When the call is omitted, behavior is unchanged.
100+
88101
<Info>
89102
**UIScene lifecycle:** Apple requires UIKit apps to adopt the UIScene lifecycle
90103
from the release following iOS 26. After migration, call

‎workmanager_apple/ios/workmanager_apple/Sources/workmanager_apple/BackgroundWorker.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class BackgroundWorker {
158158

159159
var flutterEngine: FlutterEngine? = FlutterEngine(
160160
name: backgroundMode.flutterThreadlabelPrefix,
161-
project: nil,
161+
project: WorkmanagerPlugin.dartProject,
162162
allowHeadlessExecution: true
163163
)
164164

‎workmanager_apple/ios/workmanager_apple/Sources/workmanager_apple/WorkmanagerPlugin.swift‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public class WorkmanagerPlugin: WorkmanagerPluginBase, FlutterPlugin, Workmanage
4545
/// (fluttercommunity/flutter_workmanager#653).
4646
static var mainBinaryMessenger: FlutterBinaryMessenger?
4747

48+
static var dartProject: FlutterDartProject?
49+
50+
/// Sets the `FlutterDartProject` used by the background engines.
51+
///
52+
/// Without this call each background engine creates its own project. Apps
53+
/// whose project settings change while the app runs (for example code push
54+
/// tools that resolve the newest downloaded patch when a project is
55+
/// created) should create one project at launch and share it with the main
56+
/// engine and with this plugin.
57+
///
58+
/// - Parameter project: The project the host app created at launch
59+
@objc
60+
public static func setDartProject(_ project: FlutterDartProject) {
61+
dartProject = project
62+
}
63+
4864
/// Sets the plugin registrant callback for background task execution.
4965
///
5066
/// This callback is used to register additional plugins when background tasks

0 commit comments

Comments
 (0)