You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat!: iOS major enhancements BGAppRefreshTask, BGProcessingTask, beginBackgroundTask, printScheduledTasks (#511)
* fix:Update Workmanager iOS because no callback in Background on iOS real device, added 30sec BGAppRefresh, Updated example #396
* added permissionhandler an requests for iOS
added alert and MaterialApp to workmanager when no iOS permissions activated
* fixed errormessage on xcode
* feat:Added check for background refresh permissions #441
* text to display task event dates (show prefs) added.
* fixed workmanager iOS Part
fixed BGProcessing
fixed inputdata in callback on task
clarified timings
* fixed warning dead code and ! check
* improved Task description (hints)
* Update README.md
* Update README.md
* Update README.md
* Format readme iOS examples
* Improve code documentation
* Cleanups in SwiftWorkmanagerPlugin.swift
* Use logInfo instead of prints and NSLog
* Log unnecessary logs only in debug mode
* Remove unnecessary logs
* Remove isInitalized flag in SwiftWorkmanagerPlugin which was not set to true anywhere
* * iOS, Rename registeriOSBackgroundProcessingTask to a generic name registerProcessingTask to be consistent with rest of the plugin and possible future Android implementation
* iOS, Rename wrongly named startOnOffTask to startOneOffTask
* * Cleanup code to make it more close to original plugin so that change size is reduced and it will make it easy to review
* Change new task identifier to be consistent with existing ones e.g. instead of app.workmanager... use be.tramckrijte...
* Documentation update
* Remove unnecessary logs, comments etc which were added in PRs which were not merged, and cleanup unnecessary code
* Revert using a custom log helper OS file to use the plugins existing shared prefs
* Bump example flutter sdk to < 4 instead of < 3
* Add task identifiers to iOS AppRefresh and ProcessingTask so that user can define task names instead of using hardcoded names
* * iOS AppRefresh task interval should be 15 minutes
* Documentation update
* Initialize should not auto open App settings if background refresh permission is not assigned.
Initialize should return result
* Continue work on task identifiers for iOS AppRefresh and ProcessingTask.
* Temporarily commented old iOS background fetch
* Fix extra commas on iOS
* New iOS feature printScheduledTasks to print details of un-executed scheduled tasks. To be used during development/debugging.
Format readme to improve readability
* iOS Periodic and processing tasks will be immediately scheduled, instead of waiting for App to go to background. Since doing on backgrounding will keep on changing earliest begin date.
* Add printScheduledTasks to example app
* Format example code
* Option to set frequency for iOS periodic tasks in AppDelegate.swift
* Add initialDelay support for Workmanager.registerProcessingTask
* Remove unnecessary WorkmanagerPlugin.registerBGProcessingTask calls from AppDelegate.swift
* Cleanup unused params from Workmanager.registerProcessingTask
* Update readme and iOS setup as per new iOS developments
* Create migration steps for iOS Workmanager.registerOneOffTask to Workmanager.registerProcessingTask
* Update iOS docs
* TODO for cleanups later
---------
Co-authored-by: Lars Huth <[email protected]>
Co-authored-by: xunreal75 <[email protected]>
Co-authored-by: Ioseph Magno <[email protected]>
Co-authored-by: delfme <[email protected]>
Copy file name to clipboardExpand all lines: IOS_SETUP.md
+23-5
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ This plugin is compatible with **Swift 4.2** and up. Make sure you are using **X
10
10
> ⚠️ BGTaskScheduler is similar to Background Fetch described below and brings a similar set of constraints. Most notably, there are no guarantees when the background task will be run. Excerpt from the documentation:
11
11
>
12
12
> Schedule a processing task request to ask that the system launch your app when conditions are favorable for battery life to handle deferrable, longer-running processing, such as syncing, database maintenance, or similar tasks. The system will attempt to fulfill this request to the best of its ability within the next two days as long as the user has used your app within the past week.
13
+
>
14
+
> Workmanager BGTaskScheduler methods `registerOneOffTask`, `registerPeriodicTask`, and `registerProcessingTask` are only available on iOS 13+
13
15
14
16

15
17
@@ -19,6 +21,9 @@ This will add the **UIBackgroundModes** key to your project's `Info.plist`:
19
21
<key>UIBackgroundModes</key>
20
22
<array>
21
23
<string>processing</string>
24
+
25
+
<!-- If you need periodic tasks in iOS 13+ you need to enable Background Fetch as well -->
> ⚠️ On iOS 13+, adding a `BGTaskSchedulerPermittedIdentifiers` key to the Info.plist for new `BGTaskScheduler` API disables the `performFetchWithCompletionHandler` and `setMinimumBackgroundFetchInterval`
56
+
methods, which means you cannot use both old Background Fetch and new `registerPeriodicTask` at the same time, you have to choose one based on your minimum iOS target version.
57
+
For details see [Apple Docs](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app)
44
58
45
59
And will set the correct *SystemCapabilities* for your target in the `project.pbxproj` file:
> ⚠️ Background fetch is one supported way to do background work on iOS with work manager: **Periodic tasks** are available on Android only for now! (see #109)
81
+
> ⚠️ Background fetch is one supported way to do background work on iOS with work manager. Note that this API is deprecated starting iOS 13, however it still works on iOS 13+ as of writing this article
82
+
83
+
> ⚠️ On iOS 13+, adding a `BGTaskSchedulerPermittedIdentifiers` key to the Info.plist for new `BGTaskScheduler` API disables the `performFetchWithCompletionHandler` and `setMinimumBackgroundFetchInterval`
84
+
methods, which means you cannot use both old Background Fetch and new `registerPeriodicTask` at the same time, you have to choose one based on your minimum iOS target version.
85
+
For details see [Apple Docs](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app)
68
86
69
87
Background fetching is very different compared to Android's Background Jobs.
70
88
In order for your app to support Background Fetch, you have to add the *Background Modes* capability in Xcode for your app's Target and check *Background fetch*:
Copy file name to clipboardExpand all lines: README.md
+100-6
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@ void callbackDispatcher() {
73
73
```
74
74
75
75
Android tasks are identified using their `taskName`.
76
-
iOS tasks are identitied using their `taskIdentifier`.
76
+
iOS tasks are identified using their `taskIdentifier`.
77
77
78
78
However, there is an exception for iOS background fetch: `Workmanager.iOSBackgroundTask`, a constant for iOS background fetch task.
79
79
@@ -93,25 +93,119 @@ Refer to the example app for a successful, retrying and a failed task.
93
93
94
94
# iOS specific setup and note
95
95
96
-
iOS supports **One off tasks** with a few basic constraints:
96
+
Initialize Workmanager only once.
97
+
Background app refresh can only be tested on a real device, it cannot be tested on a simulator.
98
+
99
+
### Migrate to 0.6.x
100
+
Version 0.6.x of this plugin has some breaking changes for iOS:
101
+
- Workmanager.registerOneOffTask was previously using iOS **BGProcessingTask**, now it will be an immediate run task which will continue in the background if user leaves the App. Since the previous solution meant the one off task will only run if the device is idle and as often experienced only when device is charging, in practice it means somewhere at night, or not at all during that day, because **BGProcessingTask** is meant for long running tasks. The new solution makes it more in line with Android except it does not support **initialDelay**
102
+
- If you need the old behavior you can use the new iOS only method `Workmanager.registerProcessingTask`:
103
+
1. Replace `Workmanager().registerOneOffTask` with `Workmanager().registerProcessingTask` in your App
104
+
1. Replace `WorkmanagerPlugin.registerTask` with `WorkmanagerPlugin.registerBGProcessingTask` in `AppDelegate.swift`
105
+
- Workmanager.registerOneOffTask does not support **initialDelay**
106
+
- Workmanager.registerOneOffTask now supports **inputData** which was always returning null in the previous solution
107
+
- Workmanager.registerOneOffTask now does NOT require `WorkmanagerPlugin.registerTask` call in `AppDelegate.swift` hence remove the call
108
+
109
+
### One off tasks
110
+
iOS supports **One off tasks** only on iOS 13+ with a few basic constraints:
111
+
112
+
`registerOneOffTask` starts immediately. It might run for only 30 seconds due to iOS restrictions.
97
113
98
114
```dart
99
115
Workmanager().registerOneOffTask(
100
116
"task-identifier",
101
117
simpleTaskKey, // Ignored on iOS
102
-
initialDelay: Duration(minutes: 30),
118
+
initialDelay: Duration(minutes: 30), // Ignored on iOS
119
+
inputData: ... // fully supported
120
+
);
121
+
```
122
+
123
+
### Periodic tasks
124
+
iOS supports two types of **Periodic tasks**:
125
+
- On iOS 12 and lower you can use deprecated Background Fetch API, see [iOS Setup](./IOS_SETUP.md), even though the API is
126
+
deprecated by iOS it still works on iOS 13+ as of writing this article
127
+
128
+
-`registerPeriodicTask` is only supported on iOS 13+, it might run for only 30 seconds due to iOS restrictions, but doesn't start immediately, rather iOS will schedule it as per user's App usage pattern.
129
+
130
+
> ⚠️ On iOS 13+, adding a `BGTaskSchedulerPermittedIdentifiers` key to the Info.plist for new `BGTaskScheduler` API disables the `performFetchWithCompletionHandler` and `setMinimumBackgroundFetchInterval`
131
+
methods, which means you cannot use both old Background Fetch and new `registerPeriodicTask` at the same time, you have to choose one based on your minimum iOS target version.
132
+
For details see [Apple Docs](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app)
133
+
134
+
To use `registerPeriodicTask` first register the task in `Info.plist` and `AppDelegate.swift`[iOS Setup](./IOS_SETUP.md). Unlike Android, for iOS you have to set the frequency in `AppDelegate.swift`. The frequency is not guaranteed rather iOS will schedule it as per user's App usage pattern, iOS might take a few days to learn usage pattern. In reality frequency just means do not repeat the task before x seconds/minutes. If frequency is not provided it will default to 15 minutes.
135
+
136
+
```objc
137
+
// Register a periodic task with 20 minutes frequency. The frequency is in seconds.
frequency: Duration(hours: 1), // Ignored on iOS, rather set in AppDelegate.swift
149
+
inputData: ... // Not supported
150
+
);
151
+
```
152
+
153
+
For more information see [BGAppRefreshTask](https://developer.apple.com/documentation/backgroundtasks/bgapprefreshtask)
154
+
155
+
### Processing tasks
156
+
iOS supports **Processing tasks** only on iOS 13+ which can run for more than 30 seconds.
157
+
158
+
`registerProcessingTask` is a long running one off background task, currently only for iOS. It can be run for more than 30 seconds but doesn't start immediately, rather iOS might schedule it when device is idle and charging.
159
+
Processing tasks are for long processes like data processing and app maintenance. Processing tasks can run for minutes, but the system can interrupt these.
160
+
iOS might terminate any running background processing tasks when the user starts using the device.
161
+
For more information see [BGProcessingTask](https://developer.apple.com/documentation/backgroundtasks/bgprocessingtask)
0 commit comments