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

See Also: NSUrlSession Members

Syntax

[Foundation.Register("NSURLSession", true)]
[ObjCRuntime.Availability(Introduced=ObjCRuntime.Platform.Mac_10_9)]
[ObjCRuntime.Availability(Introduced=ObjCRuntime.Platform.iOS_7_0)]
public class NSUrlSession : NSObject

See Also

NSUrlSessionTask
NSUrlRequest
NSUrlSessionConfiguration
NSUrlSessionDelegate

Remarks

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

Foundation.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