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 + "&registeredtimeok=" + 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

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -