ios - Alamofire data is not coming instantly for usage -
when getting data alamofire not yet loaded completely, before below code getting executed, don't know how use completion handler in it, new ios programming, please help.
func mobileverification(mobilenumber:string)->bool{ let params:[string:string] = ["mobile_num":mobilenumber] alamofire.request("*************",method:.post,parameters:params,encoding: urlencoding.httpbody).responsejson { response in var loginmessage = "" if let jdata = response.data, let utf8text = string(data: jdata, encoding: .utf8) { let json = json(data:jdata) print(json) let array = json["result"].arrayvalue print(array[0]["message"]) if let loginmessage = array[0]["message"].string{ self.view.maketoast(loginmessage) } } } if loginmessage == "success" { //performsegue(withidentifier: "reg2mobileverification", sender: nil) return true } else { self.view.maketoast("loginfailed") //print("login failed") return false } }
swift 3.0
this asynchronous request can reruns using completion block method(closure). can try returns true or false, hope you.
func mobileverification(mobilenumber:string,completion:@escaping(bool) -> void) { let params:[string:string] = ["mobile_num":mobilenumber] alamofire.request("*************",method:.post,parameters:params,encoding: urlencoding.httpbody).responsejson { response in if let jdata = response.data, let utf8text = string(data: jdata, encoding: .utf8) { let json = json(data:jdata) print(json) let array = json["result"].arrayvalue print(array[0]["message"]) if let loginmessage = array[0]["message"].string{ if loginmessage == "success" { self.view.maketoast(loginmessage) completion(true) } else { self.view.maketoast("loginfailed") completion(false) } } } } }
use of clouser:-
mobileverification(mobilenumber: "1234567890") { (result) in if result { performsegue(withidentifier: "reg2mobileverification", sender: nil) } }
Comments
Post a Comment