---
title: "urlSessionDidFinishEvents(forBackgroundURLSession:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessiondelegate/urlsessiondidfinishevents(forbackgroundurlsession:)"
---

# urlSessionDidFinishEvents(forBackgroundURLSession:)

Tells the delegate that all messages enqueued for a session have been delivered.

## Declaration

```swift
optional func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession)
```

## Parameters

- `session`: The session that no longer has any outstanding requests.

## Mentioned in

Downloading files in the background

## Discussion

Discussion In iOS, when a background transfer completes or requires credentials, if your app is no longer running, your app is automatically relaunched in the background, and the app’s UIApplicationDelegate is sent an application(_:handleEventsForBackgroundURLSession:completionHandler:) message. This call contains the identifier of the session that caused your app to be launched. You should then store that completion handler before creating a background configuration object with the same identifier, and creating a session with that configuration. The newly created session is automatically reassociated with ongoing background activity. When your app later receives a urlSessionDidFinishEvents(forBackgroundURLSession:) message, this indicates that all messages previously enqueued for this session have been delivered, and that it is now safe to invoke the previously stored completion handler or to begin any internal updates that may result in invoking the completion handler. important: Because the provided completion handler is part of UIKit, you must call it on your main thread.

## See Also

### Handling session life cycle changes

- [urlSession(_:didBecomeInvalidWithError:)](foundation/urlsessiondelegate/urlsession(_:didbecomeinvalidwitherror:).md)
