A task, like downloading a specific resource, performed in a URL session.
SDKs
- iOS 7.0+
- macOS 10.9+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
@interface NSURLSessionTask : NSObject
Overview
The NSURLSession
class is the base class for tasks in a URL session. Tasks are always part of a session; you create a task by calling one of the task creation methods on a NSURLSession
instance. The method you call determines the type of task.
Use
NSURLSession
’sdata
and related methods to createTask With URL: NSURLSession
instances. Data tasks request a resource, returning the server’s response as one or moreData Task NSData
objects in memory. They are supported in default, ephemeral, and shared sessions, but are not supported in background sessions.Use
NSURLSession
’supload
and related methods to createTask With Request: from Data: NSURLSession
instances. Upload tasks are like data tasks, except that they make it easier to provide a request body so you can upload data before retrieving the server’s response. Additionally, upload tasks are supported in background sessions.Upload Task Use
NSURLSession
’sdownload
and related methods to createTask With URL: NSURLSession
instances. Download tasks download a resource directly to a file on disk. Download tasks are supported in any type of session.Download Task Use
NSURLSession
’sstream
orTask With Host Name: port: stream
to createTask With Net Service: NSURLSession
instances. Stream tasks establish a TCP/IP connection from a host name and port or a net service object.Stream Task
After you create a task, you start it by calling its resume
method. The session then maintains a strong reference to the task until the request finishes or fails; you don’t need to maintain a reference to the task unless it’s useful for your app’s internal bookkeeping.
Note
All task properties support key-value observing.