0

I am running a short Swift script to post some variables a PHP script and then read the returned value:

  let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/createCommunity.php");

    var request = URLRequest(url:myUrl!);
    request.httpMethod = "POST";

    let postString = "communityname=\(communityName!)&code=\(communityCode)&email=\(myEmail!)";

    request.httpBody = postString.data(using: String.Encoding.utf8);

    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]

           print (json["code"]);
           print (json["email"]);
           print (json["communityname"]);


        }
        task.resume()
    }

In my PHP code below I simply want to receive the values, and then return them in an array:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$code = htmlentities($_POST["code"]);
$communityname = htmlentities($_POST["communityname"]);

$returnValue = array();

$returnValue["code"] = $code;
$returnValue["email"] = $email;
$returnValue["communityname"] = $communityname;

echo json_encode($returnValue);

?>

I know I am doing something wrong at the end of the Swift code but I don't know what.

I am getting this error for the let task line:

Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> ()' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'

2
  • Short of running it myself, is there an error? Breakpoint "json", right click, dump, see if your data is there. Also, off topic, but if you're doing php, I'd suggest a framework, such as cakephp. Commented Oct 17, 2016 at 13:51
  • Also you'll need to unwrap those Optionals. Commented Oct 17, 2016 at 13:52

1 Answer 1

2

Maybe you did not include all your code, but... are you calling task.resume()?

Try this:

let myUrl = URL(string: "http://www.quasisquest.uk/KeepScore/createCommunity.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";

let postString = "communityname=\(communityName!)&code=\(communityCode)&email=\(myEmail!)";
request.httpBody = postString.data(using: String.Encoding.utf8);

let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
    if let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject] {
        print (json["code"]);
        print (json["email"]);
        print (json["communityname"]);
    }
}

task.resume()
Sign up to request clarification or add additional context in comments.

5 Comments

I wasn't, because when I put that in it threw out an error. I have updated the code above with task.resume() in and the error message I am getting, thanks.
/Users/richdowns/Desktop/One More Round/KeepScore/KeepScore/CreateNewCommunity.swift:65:66: Invalid conversion from throwing function of type '(Data?, URLResponse?, Error?) throws -> ()' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'
I took out the 3x let commands and it just about worked - printing out this:Optional(S7LJEA) Optional(Optional(&quot;[email protected]&quot;)) Optional(Optional(&quot;testing&quot;)) why is it printing '&quot' though?
I'd check the data sent from the sever itself in that case.
sussed it. It just needed ! on the end of each object in the postString line

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.