Mobile Development


AFNetworking is a third-party framework which is used to communicate to remote web services. We use third-party here to distinguish from frameworks created by Apple.

AFNetworking is an Objective-C framework, not a Swift framework, so some additional work is needed to bring it into our Swift projects.


Installing AFNetworking can happen with CocoaPods. Described in more detail here ( We will be using CocoaPods to install AFNetworking. The podfile should include:

pod "AFNetworking"

Then run a pod install. This is described in the CocoaPods chapter, so take a look and jump back here when you're ready.

Example AFNetworking calls

The basic premise of any REST API is a data transfer using the standard HTTP verbs of GET, POST, PUT and DELETE. If you need a refresher, we covered REST here.

Code snippets of example GET and POST commands are found on the following post:

Although written primarly in Objective-C, the official documentation of AFNetworking is excellent. It will detail standard approaches to take when using AFNetworking:

AFNetworking was recently convereted to a Swift library. That library is called Alamofire. You may choose to use this as an alternative to AFNetworking, in order to find more Swift-specific examples available on the web.


Since the release of Swift, the new hotness is to use Alamofire, created by the same Matt Thomspon of AFNetworking. Here, we'll cover how to use Alamofire, if you prefer to use it instead of AFNetworking.


For installation, we normally use CocoaPods (we have a chapter on CocoaPods coming up--feel free to jump ahead and come back) but CocoaPods isn't available yet for use with Alamofire. Instead, here is a video that goes through the process of installing a third-party library:

Installing Alamofire

Why use Alamofire over NSURLSession?

The reason a developer would choose to use a third party library instead of iOS libraries is the reduction of code and simplicity of use.

Let's check out the following examples:


Notice how this GET command uses significantly less code than our previous example found in the Networking APIs?

Alamofire.request(.GET, "", parameters: ["foo": "bar"])
         .response { (request, response, data, error) in

Using this third party library enables faster development time and a more refined focus on finding solutions.


The POST retains the same code reduction as the above GET.

let parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3

Alamofire.request(.POST, "", parameters: parameters)

The Alamofire framework is very powerful and robust. We covered just the basics, which should cover the majority of initial API integration that you may encounter. Feel free to keep exploring their file, which contains many more examples for a variety of situations.


Alamofire on GitHub

AFNetworking on GitHub

Alamofire - NSHipster