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 on what that means.
This hands-on Alamofire tutorial will show how you can create HTTP requests to download and upload data from a web public API (httpbin.org) using Alamofire library.
Built on top of Foundation URL Loading System, Alamofire is an HTTP network library based on Swift, for both iOS and MacOS. The API provides a painless way to handle with a chain of requests/replies for network calls. On Alamofire documentation we can find the list of supported features:
- Chainable Request / Response Methods
- URL / JSON / plist Parameter Encoding
- Upload File / Data / Stream / MultipartFormData
- Download File using Request or Resume Data
- Authentication with URLCredential
- HTTP Response Validation
- Upload and Download Progress Closures with Progress
- cURL Command Output
- Dynamically Adapt and Retry Requests
- TLS Certificate and Public Key Pinning
- Network Reachability
- Comprehensive Unit and Integration Test Coverage
- Complete Documentation
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
We'll start by creating the basic skeleton of a Swift project with Xcode.
Now, there are at least three different ways to install Alamofire in your early created project: using Cocoapods, Carthage or manual install. As you might have guessed, we’re going to use Cocoapods. In order to do that, we must close XCode (understand why, here: 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, and it’s where we describe the dependencies we need to add to our application target. Then, open the Podfile with you favourite text editor and change it’s content to:
After this we only need to install the dependencies running:
$ pod install && open <your_project_name>.xcworkspace
Alamofire is now ready to use.
Downloading data
Now that we have properly installed Alamofire, we are free to use it, declaring the import at the top of the file (for instance in ViewController.m), as shown below. In addition, to perform a simple download just add the following method implementation:
- We start by defining the URL for our resource
- Create an Alamofire HTTP GET request (NOTE: this is a simple request, we can define many other options and configurations, you can dig more on that check out the official documentation)
- The error and response callback handlers, are defined inside the request definition and is printing the respective information
Uploading data
If you skip the download example don’t forget to import the Alamofire header file:
import Alamofire
Similarly to the download example described above, the upload request can be made like this:
- Define the URL for our resource
- Create an Alamofire HTTP POST request (NOTE: similarly to download example, this could have many other options and configurations)
- The error and response callback handlers are defined inside the request definition and is printing the respective information
Full Project Source Code
You can check out the source project on Github.