A configuration object that defines behavior and policies for a URL session.
- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 2.0+
URLSession object defines the behavior and policies to use when uploading and downloading data using an
URLSession object. When uploading or downloading data, creating a configuration object is always the first step you must take. You use this object to configure the timeout values, caching policies, connection requirements, and other types of information that you intend to use with your
It is important to configure your
URLSession object appropriately before using it to initialize a session object. Session objects make a copy of the configuration settings you provide and use those settings to configure the session. Once configured, the session object ignores any changes you make to the
URLSession object. If you need to modify your transfer policies, you must update the session configuration object and use it to create a new
For more information about using configuration objects to create sessions, see
Types of Session Configurations
The behavior and capabilities of a URL session are largely determined by the kind of configuration used to create the session.
The singleton shared session (which has no configuration object) is for basic requests. It’s not as customizable as sessions that you create, but it serves as a good starting point if you have very limited requirements. You access this session by calling the shared class method. See that method’s discussion for more information about its limitations.
Default sessions behave much like the shared session (unless you customize them further), but let you obtain data incrementally using a delegate. You can create a default session configuration by calling the default method on the URLSessionConfiguration class.
Ephemeral sessions are similar to default sessions, but they don’t write caches, cookies, or credentials to disk. You can create an ephemeral session configuration by calling the ephemeral method on the URLSessionConfiguration class.
Background sessions let you perform uploads and downloads of content in the background while your app isn’t running. You can create a background session configuration by calling the backgroundSessionConfiguration(_:) method on the URLSessionConfiguration class.