NSURLConnection to NSURLSession

NSURLConnection to NSURLSession

 got its start a decade ago, with the original release of Safari in 2003, as an abstraction on top of the Core Foundation / CFNetwork APIs. The name  actually refers to a group of the interrelated components that form the Foundation URL Loading System: , , , , , , and its namesake, .

 objects are passed to an  object. The delegate (conforming to the erstwhile informal  and  protocols) responds asynchronously as an , and any associated  are sent from the server. 

Before a request is sent to the server, the shared cache is consulted, and depending on the policy and availability, a cached response may be returned immediately and transparently. If no cached response is available, the request is made with the option to cache its response for any subsequent requests.

In the process of negotiating a request to a server, that server may issue an authentication challenge, which is either handled automatically by the shared cookie or credential storage, or by the connection delegate. Outgoing requests could also be intercepted by a registered  object to seamlessly change loading behavior as necessary.

For better or worse,  has served as the networking infrastructure for hundreds of thousands of Cocoa and Cocoa Touch applications, and has held up rather well, considering. But over the years, emerging use cases–on the iPhone and iPad, especially–have challenged several core assumptions, and created cause for refactoring.

At WWDC 2013, Apple unveiled the successor to : .

NSURL *URL = [NSURL URLWithString:@"https://example.com/upload"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSData *data = ...; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // ... }]; [uploadTask resume];

Kalidoss Shanmugam

Mobile Architect | Technical Lead | Swift | SwiftUI | UITesting | Espresso | Kotlin | React Native | Flutter | Technical Blogger

8 年

Hi BhuShan PaWaR, Great Article, Thanks for your time.

回复
Rajan Maheshwari

Tech Lead(iOS) at Hindustan Times

8 年

Hi Try to post all the codes in swift rather than objective c that will be helpful for all.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了