Last active
March 30, 2017 12:34
-
-
Save paulmars/5b75ccb008d1db65d762466613709362 to your computer and use it in GitHub Desktop.
Swift Signup Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SignupViewController.swift | |
// | |
// Created by Paul McKellar on 10/3/16. | |
// Copyright © 2016 Paul McKellar. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class SignupViewController: UIViewController, UITextFieldDelegate { | |
var nameText: UITextField! | |
var passwordText: UITextField! | |
var signupButton: UIButton! | |
var backButton: UIButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor.white | |
let padding = CGFloat(10) | |
let corderRadius = CGFloat(2.0) | |
self.nameText = UITextField() | |
self.nameText.delegate = self | |
self.nameText.placeholder = "name" | |
self.nameText.frame = CGRect(x: padding, y: 44.0, width: self.view.frame.width - 2*padding, height: 44.0) | |
self.nameText.layer.cornerRadius = corderRadius | |
self.nameText.layer.borderWidth = 1.0 | |
self.nameText.layer.borderColor = UIColor.lightGray.cgColor | |
self.nameText.autocapitalizationType = .none | |
self.nameText.autocorrectionType = .no | |
self.view.addSubview(nameText) | |
self.passwordText = UITextField() | |
self.passwordText.delegate = self | |
self.passwordText.isSecureTextEntry = true | |
self.passwordText.placeholder = "password" | |
self.passwordText.frame = CGRect(x: padding, y: self.nameText.frame.maxY + padding, width: self.view.frame.width - 2*padding, height: 44.0) | |
self.passwordText.layer.cornerRadius = corderRadius | |
self.passwordText.layer.borderWidth = 1.0 | |
self.passwordText.layer.borderColor = UIColor.lightGray.cgColor | |
self.view.addSubview(self.passwordText) | |
self.signupButton = UIButton() | |
self.signupButton.setTitle("Signup", for: .normal) | |
self.signupButton.setTitleColor(UIColor.lightGray, for: .normal) | |
self.signupButton.layer.cornerRadius = corderRadius | |
self.signupButton.layer.borderWidth = 1.0 | |
self.signupButton.layer.borderColor = UIColor.lightGray.cgColor | |
self.signupButton.addTarget(self, action: #selector(postUser), for: .touchUpInside) | |
self.signupButton.frame = CGRect(x: padding, y: self.passwordText.frame.maxY + padding, width: self.view.frame.width - 2*padding, height: 44.0) | |
self.view.addSubview(self.signupButton) | |
self.backButton = UIButton() | |
self.backButton.frame = CGRect(x: 0, y: self.signupButton.frame.maxY, width: self.view.frame.width, height: 44.0) | |
self.backButton.setTitle("Back", for: .normal) | |
self.backButton.setTitleColor(UIColor.lightGray, for: .normal) | |
self.backButton.addTarget(self, action: #selector(dismissSignup), for: .touchUpInside) | |
self.view.addSubview(self.backButton) | |
self.nameText.becomeFirstResponder() | |
} | |
func dismissSignup() -> Void { | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "dismissSignup"), object: nil) | |
} | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
if (textField == self.nameText) { | |
self.passwordText.becomeFirstResponder() | |
} | |
else if (textField == self.passwordText) { | |
self.view.endEditing(true) | |
self.postUser() | |
} | |
return false | |
} | |
func postUser() -> Void { | |
if (self.nameText.text == nil || self.passwordText.text == nil) { | |
return | |
} | |
let req = NSMutableURLRequest(url: URL(string:"http://blurweb.dev/api/v1/users.json")!) | |
req.httpMethod = "POST" | |
let dic = ["user": ["name": self.nameText.text!, "password": self.passwordText.text!]] | |
do { | |
let jsonData = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted) | |
req.setValue("application/json", forHTTPHeaderField: "Accept") | |
req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") | |
req.httpBody = jsonData | |
let task = URLSession.shared.dataTask(with: req as URLRequest) { (data, response, error) -> Void in | |
if error != nil { | |
print("Error signing up \(error)") | |
} else { | |
do { | |
let responseDict = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] | |
if (responseDict!["success"]!.boolValue as Bool) { | |
UserDefaults.standard.set(responseDict!["token"]!, forKey: "api_session") | |
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "signedUp"), object: nil) | |
} | |
} | |
catch let error as NSError { | |
print("json parsing failure \(error)") | |
} | |
} | |
} | |
task.resume() | |
} catch let error as NSError { | |
print(error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A basic swift signup controller. I haven't used it yet, and it needs better error handling. But I'll update it as need be.