Alamofire

Alamofire:

Alamofire is a HTTP networking based library for iOS and macOS. It is used to handle HTTP request and response. It is the wrapper class of URLSession.

Before Alamofire:

  1. You need to prepare all request by yourself
  2. Task handling is complicated
  3. Multipart request and File downloading & uploading is complicated
  4. Image caching needs to done by yourself

Uses of Alamofire:

  1. It’s incredibly easy to use and makes everything look a lot cleaner
  2. Task handling is easy.
  3. You can access data on the Internet with very little effort
  4. Upload files or data with multipart, stream easily.
  5. Download a file or resume a download already in progress.
  6. You can use AlamofireImage to download and cache images

Work with Alamofire:

Alamofire library provides you an easy way to handle all network related task. It reduces the efforts of developer because developer need not to directly deal with URLSession . Alamofire uses  URLSession internally. Alamofire calls for a similar approach in that, one creates a router by conforming to a protocol, URLRequestConvertible. Under the hood, Alamofire calls for a singleton pattern that’s built on top of an URLSessionConfiguration. Everything with Alamofire is asynchronous.

1. Data Request with and without Alamofire:

Here is the code to send request with  URLSession :

  1. Create an object of URLSessionConfiguration for session Configuration.
  2. Create object of session class using URLSessionConfiguration object.
  3. Prepare URLRequest object using your url and set request type in httpMethod .
  4. Convert Dictionary to JSON using JSONSerialization.data()  and add it in request body and add other parameters for request header.
  5. Make an object of task using session.dataTask(with: request, comletionHandler: nil)
  6. Now resume the task to start execution.
  7. In completion handler, you receive the response. Do whatever you want to do with this data 🙂

If you use alamofire then you need not to do all the steps. You just need to follow few of them.

  1. Create URLSessionConfiguration object and set it’s other properties
  2. Create object of SessionManager using  URLSessionConfiguration object. It internally use URLSession.
  3. Prepare request using request method of SessionManager. You need to pass url, http method type, request body, encoding and header. It internally creates object of URLRequest and make an task and add it in request queue.
    Here responseData have a completion handler. If request is completed and response is received then it calls the completion handler and remove that request from request queue.
  4. Check if data is received successfully. If yes, then do anything with your data.

2. File Uploading with and without Alamofire:

File uploading with only URLSession is little bit complicated. In this you need to prepare multipart request by yourself.

  1. Make request object with caching policy and timeout. Define its http method.
  2. Add Content-type in request body.
  3. Create data object for request body and add content boundary and other parameters.
    Now Convert image into data and append boundary, content-type, file name, and file data in data object. Repeat it for all images or files. Append boundary in last.
  4. Create URLSession object to send request.
  5. Add data in request body.
  6. Create task and start task execution.
  7. In completion handler of data task you get data, response and error. You can work anything with it.

In the given code, you can not get progress for uploading.
If you want to send a single file then you can use uploadTask of URLSession. But through this you can not send multiple files in single request. To send a single file you need to call given method:
urlSession.uploadTask(with: request, from: dataForm)
This method only send request and not let you if file is uploaded successfully. For that you need to implement URLSessionDataDelegate methods to know the response and progress of  your request.

File uploading with alamofire is very easy. Here you need not maintain any queue for task.

To upload the file you need to follow some easy steps:

  1. Convert your image or file in Data
  2. Call Alamofire.upload() method to upload data in multipart request. It appends your data and related information in multipartFormData and take url of server where you want to upload and header as argument. It maintains queue and send multipart request.
  3. In enclosingCompletion Handler, we receive result which tells whether it is success or failure. In success blog we can check the upload status.

3. File Downloading with and without Alamofire:

File downloading without Alamofire is a complicated task. Please have a look down:

By Only URLSession you first create session configuration and then downloadTask with url and pass self as delegate. To get progress you need to confirm protocol  URLSessionDownloadDelegate  and implement given three methods to get response success, error or progress.

Using alamofire it become easy to download any file.

 

4. Image Downloading and caching:

For downloading and caching you can use AlamofireImage pod. Using this library you do not need to cache by yourself. This library will download and cache your images.

For that you need to add AlamofireImage in your pod file and install it.
Now import it in your project.
Create an object of   ImageDownloader  add pass the given parameters:

Here you can pass download priority i.e. fifo or .lifo as you want, maximum concurrent downloads and object of AutoPurgingImageCaching which take caching memory capacity as argument.

Now you can use that object of ImageDownloader to download image.

This method first check your image in cache. If it exist then return the image else it download the image, add it in cache and return it.

You can compare both the code and decide which one you want to use.

Steps to use Alamofire in your project:

  1. Import Alamofire:
    1. Add pod file using pod init command in terminal.
    2. Open pod file in editor and add given line in it:
    pod 'Alamofire’  to add Alamofire library
    or
    pod 'AlamofireImage’  for image downloading and caching.
    3. Open terminal add run command pod install to install library.
    This will add the latest version of Alamofire library in your project. Now you can use it.
  2. Use Alamofire in your project:
    When alamofire is installed in your project, so you can import it in your class where you prepare and send request. The way to use this Library is given above. Enjoy you request and response data with alamofire. 🙂

For more information please visit:

https://github.com/Alamofire/Alamofire

Leave a Reply