A few weeks ago, AFNetworking reached version 1.0, an important milestone for every software project. In this quick tip, I will quickly introduce you to AFNetworking and show what version 1.0 has to offer.
What is AFNetworking?
The README of the AFNetworking GitHub repository sums it up nicely.
AFNetworking is a delightful networking library for iOS and Mac OS X. It's built on top of
NSOperation, and other familiar Foundation technologies. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use.
As the README mentions, the most important component of AFNetworking is its community and I couldn't agree more. Whenever I consider an open-source library for a project, I look for three things:
- How active the project is with regard to development
- If the project is well documented and understandable
- The level of community involvement. Can I find help and support if necessary?
In my opinion, a project maintained by a single person is more likely to get abandoned than a project with dozens of active contributors, such as AFNetworking. It is safe to say that AFNetworking won't disappear anytime soon!
Mattt Thompson (@mattt) and Scott Raymond (@sco) started AFNetworking while at Gowallo. Unfortunately, in December 2011 Gowalla was acquired by Facebook and subsequently shut down. However, Mattt Thompson didn't stop working on AFNetworking. He open-sourced the project and that's when AFNetworking really gained popularity and momentum. A wonderful community of developers has been working on and contributing to AFNetworking over the past year and this has resulted in the release of version 1.0 in October 2012.
As I wrote previously, networking is hard, especially on mobile devices. The majority of mobile applications have a network component of some sort in order to fetch or post data to a web service. It is therefore not surprising that AFNetworking is one of the most popular repositories on GitHub in the Objective-C category. It is gradually replacing the popular ASIHTTPRequest library and giving developers a reliable and wonderful networking library to work with.
If you are new to AFNetworking, you must be wondering why you should be using AFNetworking instead of
NSURLConnection or any of the other native networking libraries that are available with the iOS SDK. Let's take a look at what AFNetworking has to offer.
Aside from AFNetworking's extensive feature set, there are two important benefits to working with AFNetworking. The first benefit is ease of use. AFNetworking makes extensive use of blocks, and this makes its API's simple, elegant, and, dare I say, beautiful! Even though AFNetworking is built on top of
NSURLConnection, as a developer, you don't need to worry about delegate callbacks when working with AFNetworking. By making use of success and failure blocks, your networking code will be concise, more readable, and generally easier to maintain. The core classes of AFNetworking are based on
NSOperation, which gives AFNetworking a lot of power and flexibility in terms of scheduling and queueing requests as well as pausing and cancelling requests.
The second important benefit of AFNetworking is its modularity. Even though AFNetworking ships with a number of specialized classes for handling common tasks, such as fetching and processing JSON data from a web service, it is easy to extend AFNetworking to fit your application's needs.
What do you get for free when you choose AFNetworking? I won't cover every feature of this awesome library, but I do want to highlight the features that really make AFNetworking shine.
AFURLConnectionOperation is the core component of AFNetworking in terms of request handling. Internally,
NSURLConnection. As I mentioned earlier, the core classes of AFNetworking are subclasses of
NSOperation. This means that requests can be queued, paused, and canceled when needed.
AFHTTPRequestOperation is a subclass of
AFURLConnectionOperation and is aimed at handling HTTP(S) requests specifically.
AFImageRequestOperation are subclasses of
AFHTTPRequestOperation. Each of these subclasses takes care of handling a specific type of request. Even though these specialized classes work out of the box, it is easy to subclass
AFHTTPRequestOperation to create a subclass that perfectly fits the needs of your application.
If your application talks to a web service, then you are going to love
AFHTTPClient. This class provides a convenient interface for interacting with a web service. By subclassing
AFHTTPClient, you can specify default headers for each request, manage authentication, construct multipart form requests, and much more.
AFHTTPClient also has the ability to monitor and respond to network changes just like Apple's reachability class does.
The AFNetworking library also ships with a category on
UIImageView, which I discussed a few months ago. This lightweight category makes loading images fast and easy without compromising the responsiveness of the main thread.
In version 1.0, you can also mark a request operation as a background task, which means that the operation will continue to run in the background when the application is moved to the background.
Originally, AFNetworking did not support ARC out of the box. However, with the 1.0 release, AFNetworking now offers full ARC support by default.
As I mentioned at the beginning of this article, the community of an open-source project is often pivotal to its success. Not only is it important to keep development active, but developers are often reluctant, and rightly so, to use libraries that aren't backed by a healthy community for help and support. AFNetworking has an awesome community and Matt Thompson even answers questions on Stack Overflow about AFNetworking.
In addition, the documentation and Wiki of AFNetworking are a great resource to get up and running with AFNetworking. Thanks to the popularity of AFNetworking, there are also a number of excellent tutorials that will help you get up to speed quickly.
Even though earlier versions of AFNetworking were compatible with iOS 4.3, version 1.0 requires either iOS 5.0 (or higher) or OS X 10.7 (or higher).
AFNetworking is not just another library with a few utility classes. It is a robust yet flexible library that has proven its value and reliability. If you are looking for a networking library for your next iOS or OS X project there are plenty of reasons to choose this gem!
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post