Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import UIKit
  2. import Foundation
  3.  
  4. class Signup: UIViewController {
  5.  
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8.  
  9. // Do any additional setup after loading the view.
  10.  
  11. }
  12.  
  13. @IBOutlet weak var username: UITextField!
  14. @IBOutlet weak var email: UITextField!
  15. @IBOutlet weak var password: UITextField!
  16. @IBOutlet weak var lastname: UITextField!
  17. @IBOutlet weak var firstname: UITextField!
  18.  
  19. @IBAction func register(_ sender: AnyObject) {
  20.  
  21. if((username.text?.isEmpty)! || (password.text?.isEmpty)! || (email.text?.isEmpty)! || (lastname.text?.isEmpty)! || (firstname.text?.isEmpty)!){
  22.  
  23. username.attributedPlaceholder = NSAttributedString(string: username.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
  24. email.attributedPlaceholder = NSAttributedString(string: email.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
  25. password.attributedPlaceholder = NSAttributedString(string: password.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
  26. lastname.attributedPlaceholder = NSAttributedString(string: lastname.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
  27. firstname.attributedPlaceholder = NSAttributedString(string: firstname.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
  28.  
  29. }else{
  30. let url = NSURL(string: "http://localhost/classdbfiles/register.php")!
  31. let request = NSMutableURLRequest(url: url as URL)
  32. request.httpMethod = "POST"
  33. let body = "username=(username.text!.lowercased())&email=(email.text!)&password=(password.text!)&lastname=(lastname.text!)&firstname=(firstname.text!)"
  34. request.httpBody = body.data(using: String.Encoding.utf8)
  35.  
  36. URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {(data: Data?, response: URLResponse?, error: Error?) in
  37.  
  38. if error == nil{
  39. DispatchQueue.main.async(execute: {
  40. do{
  41. let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
  42. guard let parseJSON = json else{
  43. print("error while parsing")
  44.  
  45. return
  46.  
  47. }
  48. let id = parseJSON["id"]
  49. if id != nil{
  50. print(parseJSON)
  51. }
  52. }catch{
  53. print("caught an error:(error)")
  54. }
  55. })
  56. }else{
  57. print("error: (error)")
  58. }
  59. }).resume()
  60. }
  61. }
  62.  
  63. override func viewDidAppear(_ animated: Bool) {
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70. override func didReceiveMemoryWarning() {
  71. super.didReceiveMemoryWarning()
  72. // Dispose of any resources that can be recreated.
  73. }
  74.  
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement