How to create a wrapper for Alamofire and SwiftyJSON? Swift – iOS

This blogpost updated with Swift 3 – Xcode 8 – iOS 10 – Alamofire 4.0

AFWrapper
AFWrapper

I have posted a basic tutorial about Alamofire and SwiftyJSON How to use Alamofire and SwiftyJSON with Swift?

And some day before we have learned about Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS

Let’s combine both the topics to make a WRAPPER of Alamofire and SwiftyJSON.

Let’s Begin to Wrap

  1. Create an NSObject class
  2. Go to File > New > File… and add a Swift NSObject class

  3. Import respective modules/frameworks
  4. Import the frameworks that you want to use with this wrapper.

  5. Create a Function
  6. Create a function with the use of Completion Handler/Closures/Blocks

    Following is a sample of GET URL call with Alamofire and response object will be return to a success or failure closure/block respectively. If response is success then return a SwiftyJSON object with the success block.

That’s It. Let’s Use It

By this way we can use the code reusability.

Following is the sample with the use of the wrapper :

Gist with Alamofire-SwiftyJSON – GET and POST Requests

In addition you can also integrate the Loader/HUD with the web service call to display web service is processing right now.

Happy Coding 🙂

  • Ozcan Durak

    Hi thanks for the tutorial, Its one the best on internet, But there isn’t any Search example made with Alamofire and Swifty Json. Please do make and share with us.

    Thanks

    • Ashish Kakkad

      I have already posted link in basic tutorial.

      • Purvi

        Thanks for the code!! but i have to handle my Authorization Token. can you please help me how can i handle base url and token from the wrapper class ?

  • Rajan Maheshwari

    If for some reason if responseObject.result.isSuccess is true but the unwrapping part means the value is nil
    let resJson = JSON(responseObject.result.value!) then the code might crash, In order to handle things use if let optional chaining.

    class func requestPOSTURL(serviceName:String,parameters: [String:Any]?, completionHandler: @escaping (JSON?, NSError?) -> ()) {

    Alamofire.request(serviceName, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse) in
    switch(response.result) {
    case .success(_):
    if let data = response.result.value{
    let json = JSON(data)
    completionHandler(json,nil)
    }
    break
    case .failure(_):
    completionHandler(nil,response.result.error as NSError?)
    break
    }
    }
    }

    How to use in controller

    import SwiftyJSON
    let params = [“yourKey”:”yourValue”]

    AFWrapper. requestPOSTURL(serviceName: “kYourUrl”, parameters: params) { (response:JSON?, error:NSError?) in
    if error != nil {
    print(error?.localizedDescription)
    return
    }
    if response == nil {
    print(“Response is nil”)
    return
    }
    print(response!)
    }

    • sourabh sharma

      Hi Rajan,

      Could you please post your own tutroial bolg for us. It will be helpful for beginners.

      • Ashish Kakkad

        Thank you.

  • Nitesh

    How do you check Internet Reachability before calling API ? Can you post tutorial explaining how to handle such API calls. For example to check internet first then after API call check whether error: true or false and then data.

  • Purvi

    How can i integrate Network reachability manager?

  • Dorsaf

    Hi, Thank you for the tutorial. I am new in swift development. Can you tell me how do I call the “requestPostURL” method please in order to use it in a viewController? Thank you