ios - Alamofire HTTP Request -


i want send http post request alamofire. there code send request in sampleviewcontroller , response request. there problem got response server when want print response in sample view controller response return nil. how can check response after response server?

  public class persistencymanager {   func sendposthttprequest(baseurl: string, parameter: [string:any?], content: string) -> any? {          let url = url(string: constant.baseurl)      var result : any?          var urlrequest = urlrequest(url: url!)           urlrequest.setvalue("text/html; charset=utf-8", forhttpheaderfield: "content-type")           urlrequest.seturlencodedformdata(parameters: parameter)            alamofire.request(urlrequest).responsejson { response in              switch response.result {              case .success :                 if let json = response.result.value as? [string:any] {                 print("json: \(json)")                  result = json               }                case .failure :                result = nil              }           }         default:           break         }          return result      }     }       class sampleviewcontroller: uiviewcontroller {        @iboutlet weak var sendbutton: uibutton!       @iboutlet weak var centerview: uiview!         override func viewdidload() {             super.viewdidload()          }       @ibaction func sendrequest(_ sender: any) {          let parameters = ["key":constant.serverkey,"userid":"0" ,"action":"vitrin","step":"list","sort": nil,"filter":nil,"start":string("5"),"end":string("8")]          constant.libraryapi.parameters = parameters         constant.libraryapi.requestcontent = "string"              let response = constant.libraryapi.getapiresponse(parameters: parameters, contenttype: "string")          print(response) //this line call before response server      }     } 

you need closure calling closure in callback of alamofire when success or failed, can check if error in closure nil, os ok , can use json

   func sendposthttprequest(baseurl: string, parameter: [string:any?], content: string, closure:@escaping ((_ success:json?,_ error:nserror?) -> void)) {      let url = url(string: constant.baseurl)     var result : any?      var urlrequest = urlrequest(url: url!)     urlrequest.setvalue("text/html; charset=utf-8", forhttpheaderfield: "content-type")     urlrequest.seturlencodedformdata(parameters: parameter)      alamofire.request(urlrequest).responsejson { response in          switch response.result {          case .success :              if let json = response.result.value as? [string:any] {                 print("json: \(json)")                  closure(json,nil)             }else{                 closure(nil,nserror(domain: "error in json parsing", code: 995, userinfo: nil))             }          case .failure :             closure(nil,response.result.error)         }      }  } 

hope helps


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -