swift - How to call this in another view -
i have func
post request in library.swift. calling register.swift. sending data server, not data library.swift view. how solve this?
library().sendregisterinfo("http://tsprphones.com/php4credit/addnewuser.php", postdata: "emailaddressok=" + emailadd + "&passwordok=" + password + "®isteredtimeok=" + localtimestring + "&firstnameok=" + firstname + "&middlenameok=" + middlename + "&lastnameok=" + lastname){ result in print(result)//this should result server not working. }
this func in library.swift
func sendregisterinfo(url:string, postdata: string, completion: string -> void) { let request = nsmutableurlrequest(url: nsurl(string: url)!) request.httpmethod = "post" let poststring = postdata request.httpbody = poststring.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in guard error == nil && data != nil else { // check fundamental networking error print("error=\(error)") return } if let httpstatus = response as? nshttpurlresponse httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response)") } let needtopasstoanother = nsstring(data: data!, encoding: nsutf8stringencoding) as? string print(needtopasstoanother)//this printing } task.resume() }
in sendregisterinfo method, did not set string completion block, explains why not print in console.
func sendregisterinfo(url:string, postdata: string, completion: string -> void) { let request = nsmutableurlrequest(url: nsurl(string: url)!) request.httpmethod = "post" let poststring = postdata request.httpbody = poststring.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in guard error == nil && data != nil else { // check fundamental networking error print("error=\(error)") return } if let httpstatus = response as? nshttpurlresponse httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response)") } let needtopasstoanother = nsstring(data: data!, encoding: nsutf8stringencoding) as? string print(needtopasstoanother)//this printing } completion("it works") task.resume() }
it should print "it works" in viewcontroller class when call sendregisterinfo method.
besides, should consider using singleton call api.
Comments
Post a Comment