make alamofire wait

func newgetenckey()->String{ var enckey: String? let NissanURL = "https://gdcportalgw.its-mo.com/gworchest_0323A/gdc/InitialApp.php" let outgoing = ["RegionCode":"NNA", "initial_app_strings":"geORNtsZe5I4lRGjG9GZiA"] let header: HTTPHeaders=[ "Content-Type":"application/x-www-form-urlencoded", "Connection":"keep-alive", "Accept":"*/*", "User-Agent":"Conifer/0.1 (iPhone; iOS 11.2; Scale/2.00)", "Accept-Language":"en-US;q=1", "Accept-Encoding":"gzip,deflate", "Content-Length":"57" ] Alamofire.request(NissanURL, method: .post, parameters: outgoing, encoding: URLEncoding.httpBody, headers: header).responseJSON { responseData in if((responseData.result.value) != nil){ let swiftyJsonVar = JSON(responseData.result.value!) print(swiftyJsonVar["baseprm"].stringValue) enckey = swiftyJsonVar["baseprm"].stringValue } } return enckey! }
I want to wait for a response from alamofire before I return enckey. I don't really understand completion handlers. I dont even understand the "{ responseData in" on line 15.

4 Responses

typealias downloadComplete = () -> ()

func newgetenckey(completed: @escaping downloadComplete)->String{
var enckey: String?
let NissanURL = " https://gdcportalgw.its-mo.com/gworchest_0323A/gdc/InitialApp.php"
let outgoing = ["RegionCode":"NNA",
"initial_app_strings":"geORNtsZe5I4lRGjG9GZiA"]
let header: HTTPHeaders=[
"Content-Type":"application/x-www-form-urlencoded",
"Connection":"keep-alive",
"Accept":"*/*",
"User-Agent":"Conifer/0.1 (iPhone; iOS 11.2; Scale/2.00)",
"Accept-Language":"en-US;q=1",
"Accept-Encoding":"gzip,deflate",
"Content-Length":"57"
]
Alamofire.request(NissanURL, method: .post, parameters: outgoing, encoding: URLEncoding.httpBody, headers: header).responseJSON { responseData in
if((responseData.result.value) != nil){
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar["baseprm"].stringValue)
enckey = swiftyJsonVar["baseprm"].stringValue
}
}
completed()
return enckey!
}
@Erik now I get an error for the call:
let tempresult = newgetenckey()

Editor placeholder in source file
Missing argument for parameter 'completed' in call
Insert 'completed: <#ViewController.downloadComplete#>'

It looks like I need to pass something to the function, but I dont understand how this works so I dont know what it wants.
See if that works. I added: typealias downloadComplete = () -> () then completed: @escaping downloadComplete as a parameter, then completed()
You can also try to move the completed() in between the curly brackets above.

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.