iosrequestswift4alamofirealamofire-request

Alamofire request is not executed in swift 5


I have been using Alamofire in my whole project. Now in one of my modules, the Alamofire.request() is not getting executed. I tried putting a breakpoint and it showed that all of a sudden the request is being skipped. As I am working on a live project for security purpose am not including the URL. The code which I used is given here.

func responseFunction() {

    let url = ""
    let parametervalue = ["enq_id" : self.enquiry_id]

    print(self.enquiry_id)


    Alamofire.request(url, method: .post, parameters: parametervalue).responseJSON(completionHandler: {response in

       if let response_data = response.result.value as? NSDictionary{

         let rdata = response_data.value(forKey: "response") as! String

         if rdata == "Success"
         {
            let res_data = response_data.value(forKey: "result") as! [NSDictionary]
            print(res_data)
            for resultdata in res_data{

                let enqid = resultdata.value(forKey: "enq_id") as! String
                let userid = resultdata.value(forKey: "user_id") as! String
                let reference_no = resultdata.value(forKey: "reference_no") as! String
                let enq_date = resultdata.value(forKey: "enq_date") as! String
                let enq_time1 = resultdata.value(forKey: "enq_time") as! String
                let enq_time2 = enq_time1.replacingOccurrences(of: "+", with: " ")
                let enq_time = enq_time2.replacingOccurrences(of: "%3A", with: " : ")

            }

             self.queryResponseTable.reloadData()

         }

         else{

            let alertController = UIAlertController(title: "Response Details", message: "Server Error ! Could not get any response. Please try again later!", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
            self.present(alertController, animated: true, completion: nil)

         }
     }

    })

}

Can anyone help me out?


Solution

  • I think the below code helps you.

    func Api_withParameters(aView : UIView, aWebServiceName : String, aDictParam : Dictionary<String, Any>, completeBlock: @escaping ( _ response: AnyObject) -> Void, errorBlock: @escaping ( _ error: AnyObject) -> Void) {
    
        let passingURL = "\(BASEURL)/\(aWebServiceName)"
        let jsonData = try? JSONSerialization.data(withJSONObject: aDictParam, options: [])
        var jsonString = String(data: jsonData!, encoding: .utf8)
        jsonString = jsonString!.filter { !"\\)(\n\t\r".contains($0) }
    
        let passingParameter = [K_APIKEY:APIKEY, K_Data:jsonString!]
    
        print("***** Web-Service URL : \(passingURL) \n Passing Parameter : \(passingParameter)")
    
        Alamofire.request(passingURL, method: HTTPMethod.post, parameters: passingParameter, encoding: URLEncoding.default, headers: HEADER_CONTENT_TYPE).responseJSON { (response:DataResponse<Any>) in
            switch(response.result) {
            case .success(_):
                completeBlock(response.result.value as AnyObject)
                break
    
            case .failure(let error):
                var errorDict = [String : Any]()
                let errorcode : Int = error._code
                if let httpStatusCode = response.response?.statusCode {
                    switch(httpStatusCode) {
                    case 404:
                        errorDict = ["errormsg" : "Invalid URL: \(passingURL)", "errorCode" : httpStatusCode]
                    default: break
    
                    }
                } else {
                    if (String(errorcode) == "-1009"){
                        errorDict = ["errormsg" : "Please check your internet connection"]
                    }
                    else if(error.localizedDescription != ""){
                        errorDict = ["errormsg" : error.localizedDescription]
                    }
                    else{
                        errorDict = ["errormsg" : "Server unreachable please try again later."]
                    }
                }
                errorBlock(errorDict as AnyObject)
                break
            }
        }
    }