Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Foundation
- class Signup: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- @IBOutlet weak var username: UITextField!
- @IBOutlet weak var email: UITextField!
- @IBOutlet weak var password: UITextField!
- @IBOutlet weak var lastname: UITextField!
- @IBOutlet weak var firstname: UITextField!
- @IBAction func register(_ sender: AnyObject) {
- if((username.text?.isEmpty)! || (password.text?.isEmpty)! || (email.text?.isEmpty)! || (lastname.text?.isEmpty)! || (firstname.text?.isEmpty)!){
- username.attributedPlaceholder = NSAttributedString(string: username.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
- email.attributedPlaceholder = NSAttributedString(string: email.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
- password.attributedPlaceholder = NSAttributedString(string: password.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
- lastname.attributedPlaceholder = NSAttributedString(string: lastname.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
- firstname.attributedPlaceholder = NSAttributedString(string: firstname.placeholder!, attributes: [NSForegroundColorAttributeName: UIColor.red])
- }else{
- let url = NSURL(string: "http://localhost/classdbfiles/register.php")!
- let request = NSMutableURLRequest(url: url as URL)
- request.httpMethod = "POST"
- let body = "username=(username.text!.lowercased())&email=(email.text!)&password=(password.text!)&lastname=(lastname.text!)&firstname=(firstname.text!)"
- request.httpBody = body.data(using: String.Encoding.utf8)
- URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {(data: Data?, response: URLResponse?, error: Error?) in
- if error == nil{
- DispatchQueue.main.async(execute: {
- do{
- let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
- guard let parseJSON = json else{
- print("error while parsing")
- return
- }
- let id = parseJSON["id"]
- if id != nil{
- print(parseJSON)
- }
- }catch{
- print("caught an error:(error)")
- }
- })
- }else{
- print("error: (error)")
- }
- }).resume()
- }
- }
- override func viewDidAppear(_ animated: Bool) {
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement