<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/WidgetConfiguration/backgroundTask(_:action:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI19WidgetConfigurationPAAE14backgroundTask_6actionQrAA010BackgroundF0Vyqd__qd_0_G_qd_0_qd__YaYbcts8SendableRd__sAIRd_0_r0_lF"
  },
  "title" : "backgroundTask(_:action:)"
}
-->

# backgroundTask(_:action:)

Runs the given action when the system provides a background task.

```
nonisolated func backgroundTask<D, R>(_ task: BackgroundTask<D, R>, action: @escaping @Sendable (D) async -> R) -> some WidgetConfiguration where D : Sendable, R : Sendable
```

## Parameters

`task`

The type of task the action responds to.

`action`

The closure that is called when the system provides
a task matching the provided task.

## Discussion

When the system wakes your app or extension for one or more background
tasks, it will call any actions associated with matching tasks. When your
async actions return, the system will put your app back into a suspended
state. In Widget Extensions, this modifier can be used to handle URL Session
background tasks with [`urlSession`](/documentation/SwiftUI/BackgroundTask/urlSession).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)