Advertisement
iOS SDK

AFNetworking 1.0

by

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 NSURLConnection, 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:

  1. How active the project is with regard to development
  2. If the project is well documented and understandable
  3. 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!


Background

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.


Benefits

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.

Core Components

AFURLConnectionOperation is the core component of AFNetworking in terms of request handling. Internally, AFURLConnectionOperation uses 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.

Specialized Subclasses

AFJSONRequestOperation, AFXMLRequestOperation, AFPropertyListRequestOperation, and 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.

HTTP Client

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.

UIImageView

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.

Background Tasks

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.

ARC

Originally, AFNetworking did not support ARC out of the box. However, with the 1.0 release, AFNetworking now offers full ARC support by default.


Community

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.


Requirements

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).


Conclusion

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!

Related Posts
  • Code
    Mobile Development
    iOS: Tools of the Trade741pm preview image@2x
    There are numerous tools and services that have become indispensable in the workflow of many Cocoa developers. In this article, I will highlight some of the tools that I use as well as some alternatives.Read More…
  • Code
    iOS SDK
    Securing and Encrypting Data on iOSPs8e2e preview image@2x
    Whether you're creating a mobile application or a web service, keeping sensitive data secure is important and security has become an essential aspect of every software product. In this tutorial, I will show you how to safely store user credentials using the application's keychain and we'll take a look at encrypting and decrypting user data using a third party library.Read More…
  • Code
    iOS SDK
    Working with NSURLSession: AFNetworking 2.032pzi preview image@2x
    In the previous installments of this series, we've taken a close look at the NSURLSession API introduced in iOS 7 and OS X Mavericks. Networking on iOS and OS X has become much simpler and more flexible thanks to the NSURLSession API. Does this mean that you should stop using AFNetworking for your networking needs? And what about AFNetworking 2.0, which was introduced a few months ago? In this final installment, I will tell you about AFNetworking 2.0 and how it compares to the NSURLSession API.Read More…
  • Code
    iOS SDK
    Networking with NSURLSession: Part 1E548b preview image@2x
    From a developer's perspective, one of the more significant changes in iOS 7, and OS X Mavericks for that matter, is the introduction of NSURLSession. Even though NSURLSession may seem daunting at first glance, it's important that you understand what it is, how it relates to NSURLConnection, and what the differences are. In this series, I will take you through the fundamentals of NSURLSession so you can take advantage of this new technology in your own applications.Read More…
  • Code
    iOS SDK
    iOS 7 SDK: Core Bluetooth - Theoretical OverviewIcon ios7 v3
    The Core Bluetooth (CB) framework provides the necessary resources for iOS apps to communicate with devices that are equipped with Bluetooth Low Energy technology (BTLE). This tutorial will guide you through the evolution of CB from iOS 5 to iOS 7. Additionally, you will learn how to configure a Core Bluetooth central and peripheral, how to communicate between them, and some best programming practices.Read More…
  • Code
    Interviews
    An Interview with Nick LockwoodNick lockwood profile@2x
    In this interview, I talk to leading iOS developer Nick Lockwood about his open source contributions, his development workflow, and the educational resources he's found useful in mastering the SDK.Read More…