AFNetworking is one of the most widely used open source projects for iOS and OS X development. It powers thousands of popular and critically acclaimed apps, and serves as the foundation for dozens of other great open source libraries and frameworks. With thousands of stars and forks, and hundreds of contributors, the project is also among the most active and influential in the community.

By all accounts, AFNetworking is about as mainstream as it gets.

But have you heard about the sequel? AFNetworking 2.0.

This week on NSHipster: an exclusive look at the future of AFNetworking.

Full Disclosure: NSHipster is written by the author of AFNetworking, so this is anything but an objective account of AFNetworking or its merits. What you’re getting is the personal, honest take of AFNetworking in its current and forthcoming releases.

AFNetworking’s Big Ideas

Started in May 2011 as a humble extension of an Apple code sample from a doomed location-based social network, AFNetworking’s success was a product of timing more than anything. At a time when ASIHTTPRequest was the de facto networking solution, AFNetworking’s core ideas caught on among developers looking for a more modern solution.

NSURLConnection + NSOperation

NSURLConnection is the backbone of the Foundation URL Loading system. An NSURLConnection loads an NSURLRequest object asynchronously, calling methods on its delegates as the NSURLResponse / NSHTTPURLResponse and its associated NSData is sent to and loaded from the server; the delegate may also implement behavior for handling an NSURLAuthentication Challenge , a redirect responses, or determine how the associated NSCached URLResponse is stored to the shared NSURLCache .

NSOperation is an abstract class that models a single unit of computation, with useful constructs like state, priority, dependencies, and cancellation.

The first major breakthrough of AFNetworking was combining the two. AFURLConnection Operation , an NSOperation subclass conforms to NSURLConnection Delegate methods, and tracks the state of the request from start to finish, while storing intermediary state, such as request, response, and response data.

Blocks

iOS 4 radically improved the process of developing apps with its introduction of blocks and Grand Central Dispatch. Instead of scattering implementation logic across your application with delegates, developers could localize related functionality in block properties. Rather than struggle with a miasma of threads, invocations, and operation queues, GCD could dispatch work back and forth with ease.

What’s more, NSURLConnection Delegate methods could be customized for each request operation by setting a corresponding block property (e.g. set Will Send Request For Authentication Challenge Block: to override the default implementation of connection:will Send Request For Authentication Challenge: )

Now, an AFURLConnection Operation could be created and scheduled on an NSOperation Queue , with behavior specified on what to do with the response and response data (or any error encountered during the request lifecyle) when finished by setting the new completion Block property on NSOperation .

Serialization & Validation

Going even further, request operations could have their responsibilities extended to validate HTTP status codes and content type to validate the server response, and, for instance, serialize NSData into JSON objects for responses with an application/json MIME type.

Loading JSON, XML, property lists, or images from the server was abstracted to more closely resemble a latent file loading operation, such that a developer could think in terms of promises rather than asynchronous networking.

Introducing AFNetworking 2.0

In many ways, AFNetworking succeeded in striking a balance between ease-of-use and extensibility. That’s not to say that there wasn’t room for improvement.

With its second major release, AFNetworking aims to reconcile many of the quirks of the original design, while adding powerful new constructs to power the next generation of iOS and OS X apps.

Motivations

NSURLSession Compatibility - NSURLSession is a replacement for NSURLConnection introduced in iOS 7. NSURLConnection isn’t deprecated, and likely won’t be for some time, but NSURLSession is the future of networking in Foundation, and it’s a bright future at that, addressing many of the shortcomings of its predecessor. (See WWDC 2013 Session 705 “What’s New in Foundation Networking” for a good overview). Some had initially speculated that NSURLSession would obviate the need for AFNetworking; although there is overlap, there is still much that a higher-level abstraction can provide. AFNetworking 2.0 does just this, embracing and extending NSURLSession to pave over some of the rough spots, and maximize its usefulness.

- is a replacement for introduced in iOS 7. isn’t deprecated, and likely won’t be for some time, but is the future of networking in Foundation, and it’s a bright future at that, addressing many of the shortcomings of its predecessor. (See WWDC 2013 Session 705 “What’s New in Foundation Networking” for a good overview). Some had initially speculated that would obviate the need for AFNetworking; although there is overlap, there is still much that a higher-level abstraction can provide. Modularity - One of the major criticisms of AFNetworking is how bulky it is. Although its architecture lent itself well to modularity on a class level, its packaging didn’t allow for individual features to be selected à la carte. Over time, AFHTTPClient in particular became overburdened in its responsibilities (creating requests, serializing query string parameters, determining response parsing behavior, creating and managing operations, monitoring network reachability). In AFNetworking 2.0, you can pick and choose only the components you need using CocoaPods subspecs.

Meet the Cast

NSURLConnection Components (iOS 6 & 7)

AFURLConnection Operation - A subclass of NSOperation that manages an NSURLConnection and implements its delegate methods.

- A subclass of that manages an and implements its delegate methods. AFHTTPRequest Operation - A subclass of AFURLConnection Operation specifically for making HTTP requests, which creates a distinction between acceptable and unacceptable status codes and content types. The main difference in 2.0 is that you’ll actually use this class directly, rather than subclass it, for reasons explained in the “Serialization” section.

- A subclass of specifically for making HTTP requests, which creates a distinction between acceptable and unacceptable status codes and content types. The main difference in 2.0 is that you’ll actually use this class directly, rather than subclass it, for reasons explained in the “Serialization” section. AFHTTPRequest Operation Manager - A class that encapsulates the common patterns of communicating with a web service over HTTP, backed by NSURLConnection by way of AFHTTPRequest Operation .

NSURLSession Components (iOS 7)

AFURLSession Manager - A class that creates and manages an NSURLSession object based on a specified NSURLSession Configuration object, as well as data, download, and upload tasks for that session, implementing the delegate methods for both the session and its associated tasks. Because of the odd gaps in NSURLSession ’s API design, any code working with NSURLSession would be improved by AFURLSession Manager .

- A class that creates and manages an object based on a specified object, as well as data, download, and upload tasks for that session, implementing the delegate methods for both the session and its associated tasks. Because of the odd gaps in ’s API design, . AFHTTPSession Manager - A subclass of AFURLSession Manager that encapsulates the common patterns of communicating with an web service over HTTP, backed by NSURLSession by way of AFURLSession Manager .

So to recap: in order to support the new NSURLSession APIs as well as the old-but-not-deprecated-and-still-useful NSURLConnection , the core components of AFNetworking 2.0 are split between request operation and session tasks. AFHTTPRequest Operation Manager and AFHTTPSession Manager provide similar functionality, with nearly interchangeable interfaces that can be swapped out rather easily, should the need arise (such as porting between iOS 6 and 7).

All of the other functionality previous tied up in AFHTTPClient , such as serialization, security, and reachability, has been split out across several modules that are shared between NSURLSession and NSURLConnection -backed APIs.

Serialization

One of the breakthroughs of AFNetworking 2.0’s new architecture is use of serializers for creating requests and parsing responses. The flexible design of serializers allows for more business logic to be transferred over to the networking layer, and for previously built-in default behavior to be easily customized.

<AFURLRequest Serializer> - Objects conforming to this protocol are used to decorate requests by translating parameters into either a query string or entity body representation, as well as setting any necessary headers. Anyone who had beef about the way AFHTTPClient encoded query string parameters should find this new approach to be more to your liking.

- Objects conforming to this protocol are used to decorate requests by translating parameters into either a query string or entity body representation, as well as setting any necessary headers. Anyone who had beef about the way encoded query string parameters should find this new approach to be more to your liking. <AFURLResponse Serializer> - Objects conforming to this protocol are responsible for validating and serializing a response and its associated data into useful representations, such as JSON objects, images, or even Mantle-backed model objects. Rather than endlessly subclassing AFHTTPClient , AFHTTPRequest Operation now has a single response Serializer property, which is set to the appropriate handler. Likewise, the NSURLProtocol -inspired request operation class registration nonsense is no more—replaced by that single delightful response Serializer property. Thank goodness.

Security

Thanks to the contributions of Dustin Barker, Oliver Letterer, and Kevin Harwood and others, AFNetworking comes with built-in support for SSL pinning, which is critical for apps that deal with sensitive information.

AFSecurity Policy - A class that evaluates the server trust of secure connections against its specified pinned certificates or public keys. tl;dr Add your server certificate to your app bundle to help prevent against man-in-the-middle attacks.

Reachability

Another piece of functionality now decoupled from AFHTTPClient is network reachability. Now you can use it on its own, or as a property on AFHTTPRequest Operation Manager / AFHTTPSession Manager .

AFNetwork Reachability Manager - This class monitors current network reachability, providing callback blocks and notifications for when reachability changes.

AFEvent Source - An Objective-C implementation of the Event Source DOM API. A persistent HTTP connection is opened to a host, which streams events to the event source, to be dispatched to listeners. Messages streamed to the event source formatted as JSON Patch documents are translated into arrays of AFJSONPatch Operation objects. These patch operations can be applied to the persistent data set fetched from the server.

NSURL * URL = [ NSURL URLWith String : @"http://example.com" ]; AFHTTPSession Manager * manager = [[ AFHTTPSession Manager alloc ] init With Base URL : URL ]; [ manager GET : @"/resources" parameters : nil success :^ ( NSURLSession Data Task * task , id response Object ) { [ resources add Objects From Array : response Object [ @"resources" ]]; [ manager SUBSCRIBE : @"/resources" using Block : ^ ( NSArray * operations , NSError * error ) { for ( AFJSONPatch Operation * operation in operations ) { switch ( operation . type ) { case AFJSONAdd Operation Type : [ resources add Object : operation . value ]; break ; default: break ; } } } error : nil ]; } failure : nil ];

UIKit Extensions

All of the UIKit categories in AFNetworking 2.0 have been extracted and expanded, with several new additions to the list.

AFNetwork Activity Indicator Manager : Automatically start and stop the network activity indicator in the status bar as request operations and tasks begin and finish loading.

: Automatically start and stop the network activity indicator in the status bar as request operations and tasks begin and finish loading. UIImage View+AFNetworking : Adds image Response Serializer property, which makes it easy to automatically resize or apply a filter to images loaded remotely to an image view. For example, AFCore Image Serializer could be used to apply Core Image filters to the response image before being displayed.

: Adds property, which makes it easy to automatically resize or apply a filter to images loaded remotely to an image view. For example, could be used to apply Core Image filters to the response image before being displayed. UIButton+AFNetworking (New): Similar to UIImage View+AFNetworking , loads image and background Image from remote source.

(New): Similar to , loads and from remote source. UIActivity Indicator View+AFNetworking (New): Automatically start and stop a UIActivity Indicator View according to the state of a specified request operation or session task.

(New): Automatically start and stop a according to the state of a specified request operation or session task. UIProgress View+AFNetworking (New): Automatically track the upload or download progress of a specified request operation or session task.

(New): Automatically track the upload or download progress of a specified request operation or session task. UIWeb View+AFNetworking (New): Provides a more sophisticated API for loading URL requests, with support for progress callbacks and content transformation.

Thus concludes our whirlwind tour of AFNetworking 2.0. New features for the next generation of apps, combined with a fresh new architecture for all of the existing functionality. There’s a lot to be excited about.

Hit the Ground Running

You can start playing around with AFNetworking 2.0 by putting the following in your Podfile :

platform :ios , '7.0' pod "AFNetworking" , "2.5.0"

For anyone coming over to AFNetworking from the current 1.x release, you may find the AFNetworking 2.0 Migration Guide especially useful.