iOS Guides | Samples

Foundation.NSUrlSession Class

Coordinates a set of data-transfer tasks, including large background tasks.

See Also: NSUrlSession

Syntax

[Foundation.Register("NSURLSession", true)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.iOS, 7, 0, ObjCRuntime.PlatformArchitecture.None, null)]
[ObjCRuntime.Introduced(ObjCRuntime.PlatformName.MacOSX, 10, 9, ObjCRuntime.PlatformArchitecture.None, null)]
public class NSUrlSession : NSObject

See Also

Remarks

In iOS 7 and later, NSUrlSession and related classes are the preferred way to transfer larger files to and from the Web.

NSUrlSession downloads can work when the application is in the background.

Unlike regular tasks, background transfers are not constrained to 10 minutes, and instead will run until the transfer completes. Because background transfers are not bound by an arbitrary time limit, they can be used to upload or download large files, auto-update content in the background, and more.

The following example shows a download request, capable of asynchronously downloading a large file:

C# Example

var config = NSUrlSessionConfiguration.BackgroundSessionConfiguration(Guid.NewGuid().ToString());
var session = NSUrlSession.FromConfiguration(config, new SimpleSessionDelegate(), new NSOperationQueue());
var downloadTask = session.CreateDownloadTask(NSUrlRequest.FromUrl(url));
//Begin download, asynchronously
downloadTask.Resume();
 ... 
class SimpleSessionDelegate : NSUrlSessionDownloadDelegate
{
  public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
	{
		var file = File.OpenRead(location.Path);
     ...
	}
}

Related content

Requirements

Namespace: Foundation
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0

The members of Foundation.NSUrlSession are listed below.

See Also: NSObject

Protected Constructors

Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
ConfigurationNSUrlSessionConfiguration. The configuration object for this session.
[read-only]
DelegateINSUrlSessionDelegate. An instance of the Foundation.NSUrlSessionDelegate model class which acts as the class delegate.
[read-only]
DelegateQueueNSOperationQueue. The operation queue used when this was created. Read-only.
SessionDescriptionString. A human-readable description for the session.
[read-only]
static
SharedSessionNSUrlSession. The shared singleton object.
[read-only]
WeakDelegateNSObject. An object that can respond to the delegate protocol for this type

Public Methods

CreateBidirectionalStream(NSNetService) : NSUrlSessionStreamTask
CreateBidirectionalStream(String, nint) : NSUrlSessionStreamTask
CreateDataTask(NSUrl) : NSUrlSessionDataTask
Creates an HTTP request based on the specified url.
CreateDataTask(NSUrlRequest) : NSUrlSessionDataTask
CreateDataTask(NSUrl, NSUrlSessionResponse) : NSUrlSessionDataTask
CreateDataTask(NSUrlRequest, NSUrlSessionResponse) : NSUrlSessionDataTask
CreateDataTaskAsync(NSUrl) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateDataTaskAsync(NSUrlRequest) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateDataTaskAsync(NSUrl, out NSUrlSessionDataTask) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateDataTaskAsync(NSUrlRequest, out NSUrlSessionDataTask) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateDownloadTask(NSData) : NSUrlSessionDownloadTask
Creates a download task for a previously canceled or failed download.
CreateDownloadTask(NSUrl) : NSUrlSessionDownloadTask
CreateDownloadTask(NSUrlRequest) : NSUrlSessionDownloadTask
CreateDownloadTask(NSUrl, NSUrlDownloadSessionResponse) : NSUrlSessionDownloadTask
CreateDownloadTask(NSUrlRequest, NSUrlDownloadSessionResponse) : NSUrlSessionDownloadTask
CreateDownloadTaskAsync(NSUrl) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
CreateDownloadTaskAsync(NSUrlRequest) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
CreateDownloadTaskAsync(NSUrl, out NSUrlSessionDownloadTask) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
Downloads a url resource asynchronously to a temporary file.
CreateDownloadTaskAsync(NSUrlRequest, out NSUrlSessionDownloadTask) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
Downloads a url resource asynchronously to a temporary file.
CreateDownloadTaskFromResumeData(NSData, NSUrlDownloadSessionResponse) : NSUrlSessionDownloadTask
CreateDownloadTaskFromResumeDataAsync(NSData) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
CreateDownloadTaskFromResumeDataAsync(NSData, out NSUrlSessionDownloadTask) : System.Threading.Tasks.Task<NSUrlSessionDownloadTaskRequest>
CreateUploadTask(NSUrlRequest) : NSUrlSessionUploadTask
Creates an upload task.
CreateUploadTask(NSUrlRequest, NSData) : NSUrlSessionUploadTask
CreateUploadTask(NSUrlRequest, NSUrl) : NSUrlSessionUploadTask
CreateUploadTask(NSUrlRequest, NSData, NSUrlSessionResponse) : NSUrlSessionUploadTask
CreateUploadTask(NSUrlRequest, NSUrl, NSUrlSessionResponse) : NSUrlSessionUploadTask
CreateUploadTaskAsync(NSUrlRequest, NSData) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateUploadTaskAsync(NSUrlRequest, NSUrl) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateUploadTaskAsync(NSUrlRequest, NSData, out NSUrlSessionUploadTask) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
CreateUploadTaskAsync(NSUrlRequest, NSUrl, out NSUrlSessionUploadTask) : System.Threading.Tasks.Task<NSUrlSessionDataTaskRequest>
FinishTasksAndInvalidate()
Invalidates the object after allowing any outstanding tasks to finish.
Flush(Action)
FlushAsync() : Task
Flushes download data to disk and ensures future requests occur on a new socket.
static
FromConfiguration(NSUrlSessionConfiguration) : NSUrlSession
Factory method to create an NSUrlSession from the specified configuration.
static
FromConfiguration(NSUrlSessionConfiguration, INSUrlSessionDelegate, NSOperationQueue) : NSUrlSession
static
FromConfiguration(NSUrlSessionConfiguration, NSUrlSessionDelegate, NSOperationQueue) : NSUrlSession
static
FromWeakConfiguration(NSUrlSessionConfiguration, NSObject, NSOperationQueue) : NSUrlSession
GetAllTasks(NSUrlSessionAllPendingTasks)
GetAllTasksAsync() : System.Threading.Tasks.Task<NSUrlSessionTask[]>
GetTasks(NSUrlSessionPendingTasks)
Requests the groups of pending tasks (data, upload and downloads).
GetTasks2(NSUrlSessionPendingTasks2)
GetTasks2Async() : System.Threading.Tasks.Task<NSUrlSessionActiveTasks2>
Requests the groups of pending tasks (data, upload and downloads).
GetTasksAsync() : System.Threading.Tasks.Task<NSUrlSessionActiveTasks>
Requests the groups of pending tasks (data, upload and downloads).
InvalidateAndCancel()
Cancels all outstanding tasks and invalidates the session object.
Reset(Action)
ResetAsync() : Task
Performs a full resets of the url session.