1

I have a function that send two string values "Name and Text" to a server. When the post method is sent the php file send an email with this two values.

Everything works if I send only one value from my iOS app however, if I try to send two or more value I don't get any email.

The code I am using is:

func postToServerFunction() {
    var textFromapp: NSString = sendValueText.text
    var nameFromapp: NSString = sendValueName.text
    println("Button Pressed")
    var url: NSURL = NSURL(string: "http://example.com//iOS/send_ios.php")!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
    var bodyData = "data=" + (textFromapp as String) //data to send
    var bodyName = "name=" + (nameFromapp as String) //data to send
    request.HTTPMethod = "POST"
    request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
    request.HTTPBody = bodyName.dataUsingEncoding(NSUTF8StringEncoding);
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
        {
            (response, data, error) in
            println(response)

            if let HTTPResponse = response as? NSHTTPURLResponse {
                let statusCode = HTTPResponse.statusCode

                if statusCode == 200 {
                    // Yes, Do something. 
                }
            }

    }


}

How could I tell the HTTPBody to look at both values?

SNos
  • 3,430
  • 5
  • 42
  • 92

2 Answers2

4

for Future reference here how I solved it:

var bodyData = "data=" + (textFromapp as String) + "&name=" + (nameFromapp as String)

any other suggestion is welcome

SNos
  • 3,430
  • 5
  • 42
  • 92
0

Look at this answers. Fun(ctional) is the most beautiful.

function encodeData(data) {
    return Object.keys(data).map(function(key) {
        return [key, data[key]].map(encodeURIComponent).join("=");
    }).join("&");
}  
Community
  • 1
  • 1
Skamielina
  • 752
  • 6
  • 22
  • Thank you very much.. but this us for java script I am looking for a solution for Swift – SNos Jul 27 '15 at 20:57