Pass string with ampersand (&) from iOS to PHP/MySQL with NSURLSession -
i'm passing large set of strings server ios (swift) php file via post. unfortunately, if user places ampersand (&) in field, rest of field lost. understand why (& signifies next field in message) i'm not sure how fix.
func uploadtosql(plan: lessonplan, isnew: bool, callback: ((data: nsdata!, response: nsurlresponse!, error: nserror!) -> void)?) { let url = nsurl(string: "https://myserver.com/receive.php")! var request:nsmutableurlrequest = nsmutableurlrequest(url:url) var bodydata = "author=\(plan.author)&title=\(plan.title)&grade=\(plan.grade)&date=\"\(plan.date)\"&element1=\(plan.element1)&objective1=\(convertedfields[0])&element2=\(plan.element2)&objective2=\(convertedfields[1])&element3=\(plan.element3)&objective3=\(convertedfields[2])&coreprocess1=\(plan.coreprocess1)&corestrand1=\(plan.corestrand1)&corestandard1=\(plan.corestandard1)&coreprocess2=\(plan.coreprocess2)&corestrand2=\(plan.corestrand2)&corestandard2=\(plan.corestandard2)&coreprocess3=\(plan.coreprocess3)&corestrand3=\(plan.corestrand3)&corestandard3=\(plan.corestandard3)&media1=\(plan.media1)&media2=\(plan.media2)&media3=\(plan.media3)&media4=\(plan.media4)&media5=\(plan.media5)&media6=\(plan.media6)&repertoire1=\(convertedfields[3])&repertoire2=\(convertedfields[4])&repertoire3=\(convertedfields[5])&process=\(convertedfields[6])&assessment=\(convertedfields[7])&statecat1=\(plan.statecat1)&statecat2=\(plan.statecat2)&statecat3=\(plan.statecat3)&statestand1=\(plan.statestand1)&statestand2=\(plan.statestand2)&statestand3=\(plan.statestand3)&comment=\(plan.comment)&shared=\(sharedint!)&authorname=\(plan.authorname)" + planid request.httpmethod = "post" request.httpbody = bodydata.datausingencoding(nsutf8stringencoding) let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in if error != nil { println("error=\(error)") return } println("response = \(response)") callback?(data: data, response: response, error: error) return } task.resume() }
typically webrequest uses % followed ascii hex value. hex value & 26, %26
google.com/#q=red%26white example of google search red&white, uses replacement
its called url encoding or percent encoding , here question answer on how more broad url encoding: stackoverflow.com/questions/24551816/swift-encode-url
note have url encode each element interpolating string, not whole result string, want keep &'s separate parameters.
Comments
Post a Comment