ios - Swift: error when trying to communicate with web API -
ios - Swift: error when trying to communicate with web API -
i trying build basic ios app using swift - first time doing so. whilst bulk of has been straight forward. have had total nightmare getting app talk api, or site in general!
this code i'm using, , code erroring:
nsurlconnection.sendasynchronousrequest(request, queue: queue, completionhandler:{ (response: nsurlresponse!, data: nsdata!, error: nserror!) -> void in println(nsstring(data: data, encoding: nsutf8stringencoding)) }) it's 'nsstring(data: data, encoding: nsutf8stringencoding)' errors
i've gone through many tutorials can find, , homecoming me same problem. error saying:
nsurlerrordomain - code: 4294966291
i 'exc_bad_instruction' error with: exc_1386_invop, sub code = 0x0 (which hasn't led me solution...
which makes me think haven't done right in set up, or should turned on in preferences, or along lines opposed code...
this whole code:
var url: nsurl = nsurl(string: "http://stackoverflow.com") var request:nsmutableurlrequest = nsmutableurlrequest(url:url) request.httpmethod = "post" var bodydata = "key1=value&key2=value&key3=value" println(url) println(bodydata) request.httpbody = bodydata.datausingencoding(nsutf8stringencoding); allow queue:nsoperationqueue = nsoperationqueue() nsurlconnection.sendasynchronousrequest(request, queue: queue, completionhandler:{ (response: nsurlresponse!, data: nsdata!, error: nserror!) -> void in println(nsstring(data: data, encoding: nsutf8stringencoding)) }) update:
using rob's code, know error printed:
sendasynchronousrequest error: error domain=nsurlerrordomain code=-1005 "the network connection lost." userinfo=0x7beafcc0 {nserrorfailingurlstringkey=http://stackoverflow.com, _kcfstreamerrorcodekey=57, nserrorfailingurlkey=http://stackoverflow.com, nslocalizeddescription=the network connection lost., _kcfstreamerrordomainkey=1, nsunderlyingerror=0x7d154ca0 "the network connection lost."}
it sounds data nil. examine contents of error object, , should give indication of why. likely, there's wrong request, unless can reproduce problem, it's hard diagnose.
anyway, if want perform request little more diagnostic info (log error if there problem, log statuscode , response if status code not 200, etc.):
nsurlconnection.sendasynchronousrequest(request, queue: queue) { response, data, error in if info == nil { println("sendasynchronousrequest error: \(error)") homecoming } if allow httpresponse = response as? nshttpurlresponse { allow statuscode = httpresponse.statuscode if statuscode != 200 { println("sendasynchronousrequest status code = \(statuscode); response = \(response)") } } println(nsstring(data: data, encoding: nsutf8stringencoding)) } in revised question, shared resulting nserror, reported -1005 (which, in retrospect, same 4294966291 error code showed earlier; latter beingness the unsigned integer representation of -1005). error (as description describes) nsurlerrornetworkconnectionlost (which kcfurlerrornetworkconnectionlost), documentation says, "returned when client or server connection severed in middle of in-progress load."
unfortunately, error used various array of issues, , i'm unable reproduce error describe, it's hard diagnose.
one thing missing request specification of content-type:
request.setvalue("application/x-www-form-urlencoded", forhttpheaderfield: "content-type") that wouldn't cause problem describe, it's practice inform server of type of request.
ios swift nsurlconnection nsurlerrordomain exc-bad-instruction
Comments
Post a Comment