1

I am trying to work with a swift code to pass vars to php and a response back to swift. this all goes smooth with the following code. This is just a simple peice of code to get things going. It gives me the correct connection and results, how ever i have to pass loads of data which should be in an array i guess. But when i try to send through more data in a array i don't see anything. In php i would explode the results to get them one by one but how do i get loads of values to variables so i can use them again?

below is my code

<?php
require('conn.php');
header('Content-type: application/json');
if($_POST) {
    $database =trim ($_POST['database']);
    $engine = trim($_POST['engine']);


    $name = "William";


    $results = Array("name" => $name
                   );



    echo json_encode($results);
}/*end if POST*/

?>

this is the swift code

let data:NSString = ("bfdprofile" as NSString)
            let engine:NSString = "account" as NSString

            self.usernameLabel.text = prefs.valueForKey("USERNAME") as? String




            let url = NSURL(string:"xxxxx.php")
            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 = "data=\(data)&engine=\(engine)"
            let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
            request.HTTPBody = requestBodyData

            // set content length
            //NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)

            var response: NSURLResponse? = nil
            var error: NSError? = nil
            let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)

            if let results = NSJSONSerialization.JSONObjectWithData(reply!, options: nil, error: &error) as? [String: String]{
            if let name = results["name"]{
                labelTestOutput.text = name as? String
                }
            }

Thanks for the help

1 Answer 1

1

You can do it asynchronously with Alamofire library as simple as that:

typealias JSONdic = [String: AnyObject]

let param = ["data": "somedata", "engine": "someEngine"]

Alamofire.request(.POST, URLString: "xxxxx.php", parameters: param).responseJSON() {
    (_,_,json,_) in
    if let json = json as? JSONdic, name = json["name"] as? String {
        // do something with name
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

it a project that is already working so i am not sure how to add it to my project. I have looked into that. I do have swiftyjson installed on my project. Can i also use that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.