Alamofire with EVReflection

 

Installation guide for Alamofire

  • Alamofire is used to make web service call from app to server in Swift.
  • It is easy to use and shorter than NSURLSession request.

Features:

  • Request/Response
  • URL/JSON Encoding
  • Upload File/Data/Stream/MultipartFormData
  • Download using request
  • Resumable downloading

Installation

  • Alamofire requires minimum deployment target of iOS 8.
    • Cocoapods
      • Open Terminal and move to Root Directory of your project.
      • Run command to install Cocoapods if not exists.
      • $ gem install cocoapods, if it returns error then use $ sudo gem install -n/usr/local/bin cocoapods
      • $ pod setup
      • $ pod init
      • $ open -a Xcode podfile
      • add pod 'Alamofire', '~> 3.4'
      • save and close file.
      • Then run $ pod install, if pod is already installed then just use $ pod update

Installation Guide for EVReflection

  • Used to support NSObject parsing from a dictionary.
  • Used to and from JSON string.
  • Support NSCoding function encodeWithCoder and decodeWithCoder
  • Mapping objects from one type to another.

Installation

  • Same step will apply here as we have done for Alamofire.
  • Now just open podfile and add pod “EVReflection”
  • Save file and close it.
  • Run $ pod update.

 

 

Now both Alamofire and EVReflection are integrated in project.

Use Alamofire with EVReflection

  1. First create a EVObject class which contains same values as response will come.
    Then create EVObject Class like:
class UserInfo: EVObject{
    var userID = "" 
    var name = "" 
}

class UserData: EVObject{ 
    var status = "" 
    var message = "" 
    var data = UserInfo? 
}
    • Now make a request with Alamofire.
    • import Alamofire
class func GetResponse(url : URLS, parameter : [String:AnyObject]?,completionHandler: Response<AnyObject, NSError> -> Void){
        if parameter != nil {
        }
        GetRequest(url, parameter: parameter).responseJSON{ (response) -> Void in
            if response.result.value != nil {
            }
            completionHandler(response)
        }
    }
    class func PostResponse(url : URLS, parameter : [String:AnyObject]?, completionHandler: Response<AnyObject, NSError> -> Void){
        
        if parameter != nil {
            
        }
        PostRequest(url, parameter: parameter).responseJSON { (response) -> Void in
            if response.result.value != nil {
                 
            }
            completionHandler(response)
        }
    }
    
    class func PostRequest(url : URLS, parameter : [String:AnyObject]?) -> Request {
        return request(.POST, mainURL + url.rawValue, parameters: parameter, encoding: .JSON)
    }
    class func GetRequest(url : URLS, parameter : [String:AnyObject]?) -> Request {
        return request(.POST, mainURL + url.rawValue, parameters: parameter, encoding: .JSON)
    }
  • Call this functions from your ViewController.
  • Make a parameter in JSON string.
  • Call your request using GET/POST/PUT/DELETE operations.
let parameter = ["data": ["username": loginEmailTxt.text!,
                "password": loginPassTxt.text!,
                "device_type": "1",
                "device_token": devToken
                ]]
RestApi.PostResponse("LOGIN_WS_URL" , parameter: parameter) { (response : Response<AnyObject, NSError>) -> Void in{
   let status = data!["status"] as! String
   if status == "1"{
       print(userDict) print(userDict.userID) print(userDict.name) 
   }
}
Want to work with us? We're hiring!