# AFNetworking

http://afnetworking.com

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.

## Installation

Installing AFNetworking can happen with CocoaPods. Described in more detail here (https://github.com/AFNetworking/AFNetworking). 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: http://swift-ios.co/using-swift-and-afnetworking-2/

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

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.

## Alamofire

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.

## Installation

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:

## 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:

## GET

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

Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.response { (request, response, data, error) in
println(request)
println(response)
println(error)
}


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

## POST

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, "http://httpbin.org/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 README.md file, which contains many more examples for a variety of situations.

## References

Alamofire on GitHub https://github.com/Alamofire/Alamofire

AFNetworking on GitHub https://github.com/AFNetworking/AFNetworking

Alamofire - NSHipster http://nshipster.com/alamofire/