Nov 21, 2022 iOS

What is NSURLSession & URL Session and Networking?

 What is NSURLSession? How is it used?

Ans: NSURLConnection became obsolete and the new Apple standard for implementing HTTP networking is NSURLSession. NSURLSession and related classes do a lot of heavy lifting for basic HTTP connection and authentication for you. It allows you to send HTTP verb (GET, POST, etc.) requests, connect to FTP, and to download files. You can option- ally configure cache and execute your requests in a background/app suspended state. Overall the structure of NSURLSession-related things looks like the following:

URL Session and Networking/ NSURLSession

The way you typically work with it is to use NSURLSessionDownloadTask objects to execute requests against given urls. It has a block-based and delegate-based API, which means there are two ways you can issue HTTP requests with

NSURLSession: either by receiving a completion handler block callback or by implementing delegate methods and receiving notifications as the data comes in. Either way is fine and has a different purpose depending on your use case (for example, if you’d like to receive download progress notification you would want to implement delegate callbacks rather than a completion block). Also NSURLSession allows you to resume, cancel, or pause networking task.

All and all, NSURLSession is a very robust way of doing HTTP and other net- working but in reality, it is a bit too low level, and in the majority of the cases, you’re better off using a wrapper library like AFNetworking or Alamofire. Both of them are the de facto standard for networking on iOS, and use NSURL- Session under the hood to run HTTP requests for you.

In iOS7, the URLSession (formerly NSURLSession) API replaces the obsolete NSURLConnection.

This tutorial will explain URLSession, URLSessionTask, and how to utilise Swift 4. This tutorial will presume that you are making an HTTP request using URLSession and receiving a JSON response in the completion block. According to the official documentation, the URLSession class natively supports the data, file, ftp, http, and https URL protocols.

The URLSession API is more user-friendly and up to date. It is compatible with iOS, tvOS, macOS, and watchOS. The URLSession API has several classes, one of which is URLSession. To conduct an HTTP request in Swift, you must first become acquainted with a few types.

A URLSession instance manages or coordinates the queries made by your application. A task is an instance of the URLSessionTask that represents a request. You never use the URLSessionTask class directly. A number of URLSessionTask subclasses are defined by Foundation. Each subclass has a distinct goal, such as downloading or uploading data.

The NSURLSession API recognises three sorts of sessions based on the configuration object used to create the session:

Default sessions work in the same way as other Foundation techniques for downloading URLs. They keep credentials in the user’s keychain and employ a persistent disk-based cache.
Ephemeral sessions do not save data to disc; all caches, credential stores, and so on are retained in RAM and are associated with the session. As a result, when your app invalidates the session, it is instantly erased.
Background sessions are identical to default sessions, except that all data transfers are handled by a separate process.

 

Index