ios - Request on php script with nsurlconnection -
i try send value server code have error on line:
let reply = nsurlconnection.sendsynchronousrequest(request, returningresponse:&response, error: &error)
the error : argument in call "error"
var yoururl="mylink" let url = nsurl(string:yoururl) let cachepolicy = nsurlrequestcachepolicy.reloadignoringlocalcachedata var request = nsmutableurlrequest(url: url!, cachepolicy: cachepolicy, timeoutinterval: 2.0) request.httpmethod = "post" // set content-type in http header let boundaryconstant = " v2ymhfg03esomerandomstuffhbqgzcako6jy"; let contenttype = "multipart/form-data; boundary=" + boundaryconstant nsurlprotocol.setproperty(contenttype, forkey: "content-type", inrequest: request) // set data var datastring = "my value" let requestbodydata = (datastring nsstring).datausingencoding(nsutf8stringencoding) request.httpbody = requestbodydata var response: nsurlresponse? = nil var error: nserror? = nil let reply = nsurlconnection.sendsynchronousrequest(request, returningresponse:&response, error: &error) let results = nsstring(data:reply!, encoding:nsutf8stringencoding) if let datafromstring = results!.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) { let json = json(data: datafromstring) if(json=="0"){ print("\n dati non validi"); } else{ print("\n account creato"); } }
you should never use sendsynchronousrequest blocking call. deprecated ios 9.0 onwards. instead can use nsurlsession. try out following code
var yoururl="mylink" let url = nsurl(string:yoururl) let request:nsmutableurlrequest = nsmutableurlrequest(url: url) let boundaryconstant = "v2ymhfg03esomerandomstuffhbqgzcako6jy"; let contenttype = "multipart/form-data; boundary=" + boundaryconstant nsurlprotocol.setproperty(contenttype, forkey: "content-type", inrequest: request) var datastring = "my value" request.httpmethod = "post" request.httpbody = (datastring nsstring).datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request, completionhandler: { (data, response, error) -> void in //reponse recieved //in case response string saved in data, of type nsdata do{ let str = try nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.allowfragments) as! [string:anyobject] print(str) } catch { print("json error: \(error)") } }) task.resume()
Comments
Post a Comment