Software Development

HandsOn: AFNetworking Tutorial

HandsOn: AFNetworking Tutorial

We are designing Bolina to make all HTTP Libs faster and easily integrate with each one of them so, at some point, I had the task of exploring the iOS HTTP libraries landscape. After having an overview of the most popular ones, I went on to experience how to use them. In the meantime, I put it all on paper to give a hand to anyone looking to have a feeling of what that means.

This hands-on AFNetworking tutorial will show how we can create HTTP requests to download and upload data from a web public API (httpbin.org) using the AFNetworking 3.0 library.

AFNetworking is a well-established library within the iOS developer community and has been growing ever since it’s the first release in 2011. It has a pretty solid and straightforward API and it is an open-source project, which means you can fork it, play around with it and even contribute to its development. In addition, besides supporting all NSURLSession features, AFNetworking provides serialization, reachability support, security policy management, and UIKit integration.

Installing Cocoapods

If you used Cocoapods before to manage dependencies, you probably have installed it before. If that’s the case, skip to the next part of the tutorial. If you haven't used it and want to give it a try, you need to install it. To do so, open a terminal and type:

$ sudo gem install cocoapods

 

Installing the dependency

This tutorial was developed in Objective-C, although AFNetworking can also be used in a Swift-based project. That said, everything starts with creating the basic skeleton of an Objective-C project’s. After that, there are at least three different ways to install AFNetworking in your early created project, using Cocoapods, Carthage or manual install. Since we will be using Cocoapods, we must close XCode in order to proceed (you can understand why at Cocoapods troubleshooting).

In your terminal, move to your projects directory:

$ cd path/to/your_project

and initiate the Pod dependencies management:

$ pod init

This will create a file named Podfile, in which we will describe the dependencies we need to add to our application target. Next, open the Podfile with you favorite text editor and change it’s content to:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0'

target 'TargetName' do

pod 'AFNetworking', '~> 3.0'

end

Now, we only need to install the dependencies running:

$ pod install && open <your_project_name>.xcworkspace

AFNetworking should be ready to use, now.

Downloading data

With AFNetworking properly installed, we can now use it, declaring the import at the top of the file (for instance in ViewController.m):

#import “AFNetworking.h”

To perform a simple download add the following method implementation:

  1. Define the URL for our resource
  2. Create a AFNetworking session manager (NOTE: this is a simple version of a manager, check out the official documentation for other options)
  3. Create and apply a policy to avoid caching, so it can force the manager to download the resource instead of using the one previously cached
  4. Create and start asynchronous HTTP GET request, setting the parameters and progress options to nil
  5. Handling the response, where in this case, we just print the result or failure that occurred

Uploading data

If you skip the download example don’t forget to import the AFNetworking header file:

#import “AFNetworking.h”

Similarly to the download example described above, the upload request can be made like this:

  1. Define the URL for our resource
  2. Create a AFNetworking session manager (NOTE: this is a simple version of a manager, check out the official documentation for other options). Once we are performing an upload, we need to serialize our body. The JSONSerializer will do that
  3. Create a json content dictionary to upload on body
  4. Create and start asynchronous HTTP POST request, setting the JSON object as parameters
  5. Handle the response. In this case, we just print the result or failure that occurred

Full Project Source Code

You can check out the source project on Github.

See Alamofire Tutorial

See NSURLSESSION Tutorial