Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.66 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5.     @IBOutlet weak var btnRegister: UIButton!
  6.     @IBOutlet weak var tbUsername: UITextField!
  7.     @IBOutlet weak var tbPassword: UITextField!
  8.     override func viewDidLoad() {
  9.         super.viewDidLoad()
  10.         // Do any additional setup after loading the view, typically from a nib.
  11.     }
  12.  
  13.     override func didReceiveMemoryWarning() {
  14.         super.didReceiveMemoryWarning()
  15.         // Dispose of any resources that can be recreated.
  16.     }
  17.     @IBAction func unwindToMenu(segue: UIStoryboardSegue){
  18.         tbUsername.text = ""
  19.         tbPassword.text = ""
  20.     }
  21.  
  22.     override func prepare(for segue: UIStoryboardSegue, sender: Any?){
  23.         let dest = segue.destination as? ApplicationViewController
  24.         dest?.username = tbUsername.text!
  25.        
  26.     }
  27.     //checks if a user already exists
  28.     func existsUsername(_ key : String) -> Bool{
  29.         return UserDefaults.standard.object(forKey: key) != nil
  30.     }
  31.    
  32.    
  33.    
  34.     @IBAction func login(_ sender: Any) {
  35.         let user = tbUsername.text
  36.         let password = tbPassword.text
  37.         let userDefaults = Foundation.UserDefaults.standard
  38.        
  39.         if !existsUsername(user!) {
  40.            
  41.             //username does not exists Alert
  42.             let alertController = UIAlertController(title: "Error", message: "this username does not exist", preferredStyle: .actionSheet)
  43.             let okAction = UIAlertAction(title: "ok", style: .cancel, handler: { (action:UIAlertAction!) in
  44.                 print("ok")
  45.             })
  46.             alertController.addAction(okAction)
  47.             self.present(alertController,animated : true, completion:nil)
  48.             return
  49.         }
  50.        
  51.         if  password == userDefaults.string(forKey: user!) {
  52.            
  53.            
  54.             //succesful login
  55.             performSegue(withIdentifier: "toApplicationSegue", sender: self)
  56.            
  57.         }
  58.         else{
  59.            
  60.             //wrong password Alert
  61.             let alertController = UIAlertController(title: "Error", message: "wrong password", preferredStyle: .actionSheet)
  62.             let okAction = UIAlertAction(title: "ok", style: .default, handler: { (action:UIAlertAction!) in
  63.                 print("ok")
  64.             })
  65.             let changePasswordAction = UIAlertAction(title: "change password", style: .destructive, handler: { (action:UIAlertAction!) in
  66.                 self.btnRegister.sendActions(for: UIControlEvents.touchUpInside)
  67.                
  68.             })
  69.             alertController.addAction(okAction)
  70.             alertController.addAction(changePasswordAction)
  71.             self.present(alertController,animated : true, completion:nil)
  72.            
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. import UIKit
  80.  
  81. class ApplicationViewController: UIViewController {
  82.  
  83.     @IBOutlet weak var lbWelcome: UILabel!
  84.    
  85.     var username = ""
  86.    
  87.     override func viewDidLoad() {
  88.         super.viewDidLoad()
  89.         lbWelcome.text = "welcome " + username + "!"
  90.  
  91.         // Do any additional setup after loading the view.
  92.     }
  93.  
  94.     override func didReceiveMemoryWarning() {
  95.         super.didReceiveMemoryWarning()
  96.         // Dispose of any resources that can be recreated.
  97.     }
  98. }
  99.  
  100.  
  101.  
  102. import UIKit
  103.  
  104. class RegistrierenViewController: UIViewController {
  105.  
  106.    
  107.     @IBOutlet weak var tbUsername: UITextField!
  108.     @IBOutlet weak var tbPassword: UITextField!
  109.     override func viewDidLoad() {
  110.         super.viewDidLoad()
  111.  
  112.         // Do any additional setup after loading the view.
  113.     }
  114.  
  115.     override func didReceiveMemoryWarning() {
  116.         super.didReceiveMemoryWarning()
  117.         // Dispose of any resources that can be recreated.
  118.     }
  119.    
  120.     @IBAction func register(_ sender: Any) {
  121.         let key = tbUsername.text
  122.         let value = tbPassword.text
  123.         let userDefaults = Foundation.UserDefaults.standard
  124.        
  125.         if userDefaults.string(forKey: key!) == value {
  126.        
  127.             //Alert
  128.             let alertController = UIAlertController(title: "Error", message: "this user already exists", preferredStyle: .actionSheet)
  129.             let okAction = UIAlertAction(title: "ok", style: .cancel, handler: { (action:UIAlertAction!) in
  130.                 print("ok")
  131.             })
  132.             alertController.addAction(okAction)
  133.             self.present(alertController,animated : true, completion:nil)
  134.             return
  135.         }
  136.         else{
  137.             //create new user
  138.             userDefaults.set(value, forKey: key!)
  139.                        
  140.             //go back to Login
  141.             performSegue(withIdentifier: "unwindBack", sender: self)
  142.            
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement