What are the benefits of using NSURLSession?
There are a number of new advantages and benefits of using NSURLSession:
- Uploads and Downloads can be done in the Background
- When the NSURLSession is created, a configuration option can be selected to allow background networking. This helps to save battery life, supports multitasking and allows developers to use the same delegate model as in-process transfers.
- Allows the pause and resume for network operations
- When the NSURLSession API is utilized, any networking task can be paused, stopped and restarted. There is no NSOperation subclassing required.
- Configurable container for storing network request configuration
- Each NSURLSession provides a configurable container for storing network requests. For instance, for setting an HTTP header option, we will need to only do this once and each subsequent request will utilize the same configuration
- Allows a private storage which is subclassable.
- Each NSURLSession is subclassable and one can configure a session to use private storage on a per session basis. This allows one to have private storage objects outside of the global state.
- Improved authentication handling
- When using NSURLConnection, if an authentication challenge was issued, the challenge would come back for an arbitrary request, so one would not know exactly what request was getting the challenge. However, with NSURLSession, the delegate handles the authentication.
- Rich delegate model
- NSURLConnection does have some asynchronous block based methods. However, a delegate cannot be used with them.
- Uploads and downloads allowed through the file system.
- This encourages a separation of the data (i.e. file contents) from the metadata (i.e. URL and settings)
NSURLSession
NSURLSession is designed as a replacement API for NSURLConnection. An NSURLSession is made using an NSURLSessionConfiguration with an optional delegate. After the session is established, the network requirements are satisfied by creating NSURLSessionTask.
According to Apple's URL Loading Programming Guide, you can use the
NSURLSession
API in two ways: with a system-provided delegate or with your own delegate. In general, you must use your own delegate if your app does any of the following:- Uses background sessions to download or upload content while your app is not running.
- Performs custom authentication.
- Performs custom SSL certificate verification.
- Decides whether a transfer should be downloaded to disk or displayed based on the MIME type returned by the server or other similar criteria.
- Uploads data from a body stream (as opposed to an
NSData
object). - Limits caching programmatically.
- Limits HTTP redirects programmatically.
If your app does not need to do any of these things, your app can use the system-provided delegates.
Basic sequence of method calls that your app needs to make and completion handler calls that your app receives when using NSURLSession with the system-provided delegate :
- Create the NSURLSessionConfiguration
- Create a session specifying a configuration object and a nil delegate.
- Create task objects within a session that each represent a resource request. The task objects are subclasses of
NSURLSessionTask
—NSURLSessionDataTask
,NSURLSessionUploadTask
, orNSURLSessionDownloadTask
, depending on the behavior you are trying to achieve. These objects are analogous toNSURLConnection
objects, but give you more control and a unified delegate model. - We need to have our delegate implement the required methods from the NSURLSessionDownloadDelegate protocol.
- When a task completes, the NSURLSession object calls the task's completion handler.
- When your app no longer needs a session, invalidate it by calling either either
invalidateAndCancel
(to cancel outstanding tasks) orfinishTasksAndInvalidate
(to allow outstanding tasks to finish before invalidating the object).
NSURLSession
class without providing delegates, your app must create tasks using a call that takes a completionHandler
parameter, because otherwise it cannot obtain data from the class.
Note:
NSURLSession
does not report server errors through the error parameter. The only errors your app receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host. The error codes are described in URL Loading System Error Codes
. Server-side errors are reported through the HTTP status code in the NSHTTPURLResponse
object. For more information, read the documentation for the NSHTTPURLResponse
and NSURLResponse
classes.
Step #1: Creating the NSURLSessionConfiguration
There are three ways to create an NSURLSessionConfiguration:
- defaultSessionConfiguration
- creates a configuration object that uses the global cache, cookie and credential storage objects. This configuration provides a session to look very much like the NSURLConnection.
- ephemeralSessionConfiguration
- This configuration is for "private" sessions and has no persistent storage for cache, cookie or credential storage objects
- backgroundSessionConfiguration
- This configuration is used for making networking calls from remote push notifications or while the app is suspended.
Once the NSURLSessionConfiguration is created, then the setting of various properties on it can be done as follows:
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; // restricts network operations to Wifi sessionConfig.allowsCellularAccess = NO; // sets all requests to only accept JSON responses [sessionConfig setHTTPAdditionalHeaders: @{@"Accept": @"application/json"}]; // configures timeouts & restricts app to only have one network connection to a host sessionConfig.timeoutIntervalForRequest = 30.0; sessionConfig.timeoutIntervalForResource = 60.0; sessionConfig.HTTPMaximumConnectionsPerHost = 1; |
Step #2: Creating the Session
With NSURLSession, you can create the tasks using the block based convenience methods, set up a delegate or both. For instance, if you want to download an image, then you will need to create an NSURLSessionDownloadTask.
// set the image URL NSString *imageUrl = @"http://someImage.png"; // create the default session configuration NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; // create a session using the current class as a delegate NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; Step #3: Download the image by creating a task with a completion handler
NSURLSessionTask
This is the base class for creating session tasks, which are generally one of the following subclasses.
NSURLSessionDataTask
This task is used for issuing HTTP GET requests to pull down data from servers. The data is returned in the form of NSData, which needs to be converted to the appropriate XML, JSON, etc. format.
This class is used to upload something to a web service using HTTP POST or PUT commands. The delegate for the tasks also allows you to watch the network traffic while it is being transmitted.
|
13 comments:
kenya safari masai mara
Kenya Safari Holidays Packages
Kenya Safari Tour Packages
Best safari in kenya
Best tour companies in kenya
vermeer
Hi, Thanks for sharing nice informative useful content.
Best Digital Marketing Course in Ameerpet
Digital Marketing Course in Ameerpet
Digital Marketing Training in Ameerpet
Digital Marketing Course in Hyderabad
Best Digital Marketing Course in Hyderabad
best digital marketing training institute in hyderabad
Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
CEH Training In Hyderbad
Thanks for good info.
aws training in hyderabad
And indeed, Iam just always astounded concerning the remarkable things served by you. Some of the facts on this page are undeniably the most effective I have had.
Big Data Hadoop Training in Chennai
Advanced Linux Training in Chennai
Cloud Computing Training in Chennai
Top Software Testing Training in Chennai
Blue Prism Training in Chennai
Angularjs Training in Chennai
MCSE Training in Chennai
AI Training in Chennai
SEO Training in Chennai
Great post. It was very informative. Home lift
Thanks for sharing such a wonderful blog
Which of the Following Numbers is not Divisible by 14
Error the Specified Value for Setting Medialoyout is Invalid While Installing SSMS From Command Line
BRAC Interview Questions and Answers Part1
Write the Test Cases on Prime Number With Result
Pointing Out to a Photograph Swathi said he is the Uncle of My Brothers Sister
How to Merger Two ar Static Libraries into One
Java Sample Resume 7 Years Experience
Find the Value of Log a2 bc
Hungarian Clothing
What is Javascripts Highest Interger Value that a Number can go to Without Losing Precision
Very useful and informative blog. Thank you so much for these kinds of informative blogs.
who provides seo services, web development services, logo design services, graphic design ,
digital markeing and all kind of web design, development and digital marketing services.
best website design services in gurgaon
web company in delhi
web desiging company
web design & development banner
web design & development company
web design & development services
web design agency delhi
web design agency in delhi
web design and development services
web design companies in delhi
web design company delhi
web design company in delhi
web design company in gurgaon
web design company in noida
web design company list
web design company services
web design company website
web design delhi
web design development company
web design development services
web design in delhi
web design service
web design services company
web design services in delhi
web designer company
web designer delhi
web designer in delhi
web designers delhi
web designers in delhi
web designing & development
web designing advertisement
web designing and development
web designing and development company
web designing and development services
web designing companies in delhi
web designing company delhi
web designing company in delhi
web designing company in gurgaon
web designing company in new delhi
web designing company in noida
web designing company logo
web designing company sites
web designing company websites
web designing delhi
web designing in delhi
web designing in gurgaon
web designing in gurgaon
web designing service
web designing services
web designing services delhi
Very informative. Thanks for sharing.
Best Software Service in Hyderabad
Best Bike Taxi Service in Hyderabad
Thanks for sharing such an informative post. In every blog comment, it is essential to post the relevant content. I gained some useful knowledge of things from this article. Anticipating your future posts. Thanks for sharing such an informative post. In every blog comment, it is essential to post the relevant content. I gained some useful knowledge of things from this article. Anticipating your future posts.
online pharmacy app development
Grammarly Premium Crack ensures that your text will be easy to read, efficient, reliable, and error-free. Download it now.Grammarly Premium Crack
Great post! If you're looking for an affordable and hassle-free rental option, consider the Cheapest Rent a Car in Dubai Without Deposit. This service allows you to rent a car without the upfront cost of a deposit, making it a convenient choice for travelers. Highly recommend checking out their services for a budget-friendly and smooth car rental experience!
Post a Comment