Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- @IBOutlet var tfEmail: UITextField!
- @IBOutlet var tfUsername: UITextField!
- @IBOutlet var tfPW: UITextField!
- var ret: String = "app loaded";
- @IBOutlet var label: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- print("Hello world");
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func printMessage() -> Void {
- let json = ["email" : self.tfEmail.text!,
- "username" : self.tfUsername.text!,
- "password" : self.tfPW.text!] as [String : Any];
- do {
- let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted);
- let url = URL(string: "http://localhost:9000/save")!
- var request = URLRequest(url: url);
- request.httpMethod = "POST";
- request.setValue("application/json; charset=utf8", forHTTPHeaderField: "Content-Type");
- request.httpBody = jsonData;
- let task = URLSession.shared.dataTask(with: request) {
- data, response, error in
- if error != nil {
- print("Error -> (String(describing: error))");
- self.ret = "(String(describing: error))";
- }
- if let httpResponse = response as? HTTPURLResponse {
- print("statusCode: (httpResponse.statusCode)");
- if(httpResponse.statusCode == 200) {
- self.ret = "success (httpResponse.statusCode)"
- } else {
- self.ret = "error (httpResponse.statusCode)"
- }
- }
- }
- task.resume();
- } catch {
- print(error);
- }
- }
- @IBAction func postButton(_ sender: Any) {
- printMessage()
- label.text = self.ret;
- }
Add Comment
Please, Sign In to add comment