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?