0

I'm developing an iOS app with Xcode and Swift.

I'm getting JSON data with SwiftyJSON.swift and the following code:

import UIKit

class ViewController: UIViewController {

    var dict = NSDictionary()

    @IBOutlet weak var firstLb: UILabel!
    @IBOutlet weak var secondLb: UILabel!
    var uasername = "TestUserName"

    override func viewDidLoad() {

        super.viewDidLoad()

        let urlString = "http://example.com/showUserInfo.php"

        if let url = NSURL(string: urlString) {

            if let data = try? NSData(contentsOfURL: url, options: []) {

                let json = JSON(data: data)

                print(json)

                let f = json[0]["name"].stringValue

                let s = json[0]["age"].stringValue

                firstLb.text = f

                secondLb.text = s
            }
        }
    }
}

But I want to be able to POST a value to showUserInfo.php, too.

My showUserInfo.php PHP script looks like this:

<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$root/config.php";

$username = $_POST['username'];

$stmt = $pdo->prepare('SELECT * FROM contact WHERE username = :username');
$stmt->execute(array('username' => $username));

$userData = array();

while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
$userData[] = $row;
}
echo json_encode($userData);
?>

Getting JSON data from the PHP script with my Swift code works very well when using $username = "TestUserName"; instead of $username = $_POST['username']; in my PHP script. But I want to be able to $_POST a specific username to this PHP script that is declared in my app.

Does anybody know how to do this without the need of importing a library or so?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

In default behaviour NSURL uses GET as a HTTP method. You have to use NSMutableURLRequest and set property called HTTPMethod to POST. If you want to see exactly example of using post request in Swift 2/3 visit here.

Community
  • 1
  • 1
Sebastian K
  • 146
  • 5
  • Hi @Sebastian K, thank you very much for trying to help me. Thank for the link. I know this method to POST a value. But I'm not able to combine both codes, the `get-JSON-code` and the `POST-value-code`. Do you know it? Can you show me how to do that? –  Sep 11 '16 at 09:50
  • I see that library you are using [can](https://github.com/SwiftyJSON/SwiftyJSON#work-with-alamofire) work with Alamofire. I understand that, you asked for something which won't use external library but in that case It would be the easiest way for you to adapt. – Sebastian K Sep 11 '16 at 15:31
  • Sorry for my late reply. I'll try to import Alamofire. But how do I have to modify my actual code to reach my goal? –  Sep 11 '16 at 18:48
0

Here is Alamofire and SwiftyJSON implementation

let parameters = ["username": "TestUserName"]

    Alamofire.request(.POST, "http://example.com/showUserInfo.php",parameters: parameters).responseJSON { response in
        debugPrint(response.result.value)
         let json = JSON(data: response.data!)
        print(json)

        let f = json[0]["name"].stringValue

        let s = json[0]["age"].stringValue

        firstLb.text = f

        secondLb.text = s
    }
Mohanmoorthy
  • 129
  • 5