How to Integrate Alamofire in Your Swift App Development Project?

Alamofire is an HTTP networking library written in Swift.

Previously, we were using AFNetworking as HTTP networking library with Objective-C because AFNetworking is the Objective-C version of the library. However, now, we are using Alamofire Swift, as it is a Swift version of AFNetworking.

Let’s start integrate Alamofire Swift tutorial:

To integrate Alamofire, you need to install cocoapods. (Please refer this link for install cocoapods :

Now, follow these steps:

  1. 👉 Go to Terminal

  2. 👉 command “cd” and give the path of your project and then press “Enter” key


  3. 👉 Create a pod file in your project by terminal by command “touch Podfile” and press “Enter”

    Note: Name should be “Podfile”


  4. 👉 Then, open “Podfile” by terminal command “open Podfile” and press “Enter”

  5. 👉 Type following in “Podfile”
    source ‘’
    platform :ios, ‘8.0’
    pod ‘Alamofire’, ‘~> 3.0.0’

    and close it with saving.


  6. 👉 Once you complete this process then come to the terminal again and type command “Pod install” and then press “Enter” Key.


    👉 Once, you have entered this command, you will find below code in your terminal window.


    👉 It will install Alamofire in your project. Now, you can use it easily.

    After that, you have to import Alamofire using

    import Alamofire

    Alamofire request

    Alamofire.request(.GET, strURL, parameters: nil, encoding: .URL, headers: nil)

    There are built-in Response Methods for Response Serialization, which we can use

    responseString(encoding: NSStringEncoding)
    responseJSON(options: NSJSONReadingOptions)
      responsePropertyList(options: NSPropertyListReadOptions)


    Alamofire.request(.GET, strURL, parameters: ["user_id" : "1"], encoding: .URL, headers: nil).responseData { (response) in

    In Alamofire request, we have to pass following data

    1. HTTP Method

      We have to pass Alamofire.Methods which is already defined as the public enum.

      public enum Method: String {

      We can use it like

      Alamofire.request(.GET, strURL, parameters: ["user_id" : "1"], encoding: .URL, headers: nil)
      Alamofire.request(.POST, strURL, parameters: [“name” : “jhon”], encoding: .URL, headers: nil)
      Alamofire.request(.DELETE, strURL, parameters: [“post_id” : “1”], encoding: .URL, headers: nil)
    2. Base Url

      Commonly, we define a BASEURL in the code and then add endpoint

      For ex:

      let BASEURL =

      When we go for call API we add endpoint to BASEURL
      for ex: strURL = BASEURL+”login”

    3. Parameters

      Parameters should be a dictionary of [String : AnyObject]?

      If you don’t pass parameters in API, it will be nil.

      For example:

      Alamofire.request(.GET, strURL, parameters: ["user_id" : "1"], encoding: .URL, headers: nil)
      Alamofire.request(. GET, strURL, parameters: nil, encoding: .URL, headers: nil)
    4. Parameter Encoding

      Parameters can also be encoded as JSON, URL or custom format. In Alamofire, we have enum (enumeration) for the same.

      public enum ParameterEncoding {
          case URL
          case URLEncodedInURL
          case JSON
          case PropertyList(NSPropertyListFormat, NSPropertyListWriteOptions)
          case Custom((URLRequestConvertible, [String: AnyObject]?) -> (NSMutableURLRequest, NSError?))

      Most of them, we use .URL/.JSON parameter encoding

    5. HTTP Headers

      If you need to pass Headers in a request, then you can pass it in [String : String]? format
      This should be nil, in case we not have to pass Header in a request.

      for ex:

             let headers = [
                  "Authorization": "Basic",
                  "Accept": "application/json"
              Alamofire.request(. GET, strURL, parameters: nil, encoding: .URL, headers: headers)
              Alamofire.request(.GET, strURL, parameters: nil, encoding: .URL, headers: nil)

      Uploading file by Alamofire
      By using Alamofire, we can upload any of file and Data. It also supports MultipartFormData, also shows the process of data uploaded.

      Uploading File

             Alamofire.upload(.POST, strURL, file: fileURL).progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
              }.responseJSON { (response) in

      Uploading MultipartFormData

             Alamofire.upload(.POST, strURL, multipartFormData: { (MultipartFormData) in
                  MultipartFormData.appendBodyPart(data: fileData, name: "myfile")
                  }) { (dataResult:Manager.MultipartFormDataEncodingResult) in
                      switch dataResult {
                      case .Success(let upload,_,_):
                          upload.responseJSON { response in
                              // success block
                          upload.progress { _, totalBytesRead, totalBytesExpectedToRead in
                              // progress block
                      case .Failure(let encodingError):

      Do you have any queries about anything you have seen? If yes, then you are free to ask anything through comment. we’d also love to read your comments. You can also hire Swift developer to integrate Alamofire in swift 4 project.

Author Bio
Hitesh Trivedi
Hitesh Trivedi
Designation: iOS Team Lead

Hitesh Trivedi is an iOS Team Lead at Space-O Technologies. He has over 10 years of experience in iOS app development. He has guided to develop over 100 iPhone apps with unique features and functionalities. He has special expertise in Swift and Objective-C.