Advertisement
Guest User

MainViewController

a guest
Feb 8th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.90 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  loginSignup
  4. //
  5. //  Created by Yan Rips on 08/02/2019.
  6. //  Copyright © 2019 Yan Rips. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.     private let LOGIN_TYPE = 0
  13.     private let LOGIN_TYPE_FAILED = 2
  14.     private let SIGNUP_TYPE = 1
  15.     private let STAGE_USERNAME = 0
  16.     private let STAGE_PASSWORD = 1
  17.     private var username:String = ""
  18.     private var password:String = ""
  19.     private var loginOrSignUP:Int = 0
  20.  
  21.     override func viewDidLoad() {
  22.         super.viewDidLoad()
  23.        
  24.        
  25.        
  26.        
  27.     }
  28.     override func viewDidAppear(_ animated: Bool) {
  29.         if validateUser(){
  30.             signInScreen(animation: false)
  31.         }
  32.         initBtns()
  33.     }
  34.     func initBtns(){
  35.         let loginBtn = UIButton(type: .system)
  36.         loginBtn.frame = CGRect(x: view.frame.width/2-110, y: view.frame.height/2-80, width: 220, height: 80)
  37.         loginBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
  38.         loginBtn.setTitle("Login", for: .normal)
  39.         loginBtn.addTarget(self, action: #selector(loginInAlert), for: .touchUpInside)
  40.         view.addSubview(loginBtn)
  41.  
  42.        
  43.         let signUp = UIButton(type: .system) as UIButton
  44.         signUp.frame = CGRect(x: view.frame.width/2-110, y: view.frame.height/2, width: 220, height: 80)
  45.         signUp.titleLabel?.font = UIFont.boldSystemFont(ofSize: 30)
  46.         signUp.setTitle("signUp", for: .normal)
  47.         signUp.addTarget(self, action: #selector(signUpAlert), for: .touchUpInside)
  48.        
  49.         view.addSubview(signUp)
  50.     }
  51.     @objc func loginInAlert(warningMessage:Bool){
  52.         var alert:UIAlertController
  53.         if warningMessage {
  54.             alert = initAlert(type: LOGIN_TYPE_FAILED, stage: STAGE_USERNAME)
  55.         }else{
  56.             alert = initAlert(type: LOGIN_TYPE, stage: STAGE_USERNAME)
  57.         }
  58.         present(alert, animated: true, completion: nil)
  59.     }
  60.     @objc func signUpAlert(){
  61.         let alert = initAlert(type: SIGNUP_TYPE, stage: STAGE_USERNAME)
  62.         present(alert, animated: true, completion: nil)
  63.     }
  64.     func initAlert(type:Int, stage:Int) -> UIAlertController{
  65.         var title:String = ""
  66.         var message:String = ""
  67.         var placeHolder:String = ""
  68.         var isSecure:Bool = false
  69.         var confirmBtn:String = "continue"
  70.         if(type == LOGIN_TYPE){
  71.             title = "Login"
  72.             loginOrSignUP = LOGIN_TYPE
  73.         }
  74.         if(type == SIGNUP_TYPE){
  75.             title = "Sign Up"
  76.             loginOrSignUP = SIGNUP_TYPE
  77.         }
  78.         if(stage == STAGE_USERNAME){
  79.             message = "Please enter your username"
  80.             placeHolder = "Username"
  81.             if type == LOGIN_TYPE_FAILED{
  82.                 message = "Username or password is wrong \n Please enter your user name"
  83.                 title = "Failed to login, Try again"
  84.             }
  85.         }
  86.         if(stage == STAGE_PASSWORD){
  87.             message = "please enter your password"
  88.             placeHolder = "Password"
  89.             confirmBtn = title
  90.             isSecure = true
  91.         }
  92.         let alert:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  93.    
  94.         alert.addTextField { (editText: UITextField) in
  95.             editText.placeholder = placeHolder
  96.             editText.isSecureTextEntry = isSecure
  97.            
  98.             let confirmAct = UIAlertAction(title: confirmBtn, style: .default, handler: { (alertAct: UIAlertAction) in
  99.                 if let text = editText.text{
  100.                     if(stage == self.STAGE_USERNAME){
  101.                         self.username = text
  102.                         if(type == self.LOGIN_TYPE || type == self.LOGIN_TYPE_FAILED){
  103.                             self.loginAlertPassword()
  104.                         }
  105.                         if(type == self.SIGNUP_TYPE){
  106.                             self.signUpAlertPassword()
  107.                         }
  108.                     }else if(stage == self.STAGE_PASSWORD){
  109.                         self.password = text
  110.                         self.loginOrSignupUser(username: self.username, password: self.password, loginOrSignup: self.loginOrSignUP)
  111.                     }
  112.                 }
  113.             })
  114.             let cancelAct = UIAlertAction(title: "cancel", style: .cancel, handler: nil )
  115.             alert.addAction(cancelAct)
  116.             alert.addAction(confirmAct)
  117.         }
  118.         return alert
  119.     }
  120.    
  121.     func loginAlertPassword(){
  122.         let alert = initAlert(type: LOGIN_TYPE, stage: STAGE_PASSWORD)
  123.         present(alert, animated: true, completion: nil)
  124.     }
  125.     func signUpAlertPassword(){
  126.         let alert = initAlert(type: SIGNUP_TYPE, stage: STAGE_PASSWORD)
  127.         present(alert, animated: true, completion: nil)
  128.     }
  129.    
  130.     func loginOrSignupUser(username:String, password:String, loginOrSignup:Int){
  131.         let url = URL(string: "http://192.168.1.18:8080/IOSLoginTest_war_exploded/servlet?login=\(loginOrSignup)&username=\(username)&password=\(password)")
  132.         let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
  133.             if let data = data {
  134.                 let message:String = String(data: data, encoding: String.Encoding.utf8)!
  135.                 if(message.contains("success")){
  136.                     self.signInAndChangeScreen(username: username, password: password)
  137.                 }else{
  138.                     if(self.loginOrSignUP == 0){
  139.                         self.loginInAlert(warningMessage: true)
  140.                     }else{
  141.                         self.signUpAlert()
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         task.resume()
  147.                     // Convert the data to JSON
  148.        
  149.        
  150.        
  151.     }
  152.     func signInAndChangeScreen(username:String, password:String){
  153.         if self.saveUserInCache(user: username, pass: password){
  154.             self.signInScreen(animation: true)
  155.         }
  156.     }
  157.     func saveUserInCache(user:String, pass:String) -> Bool{
  158.         let preferences = UserDefaults.standard
  159.         let usernameKey = "username"
  160.         preferences.set(user, forKey: usernameKey)
  161.        
  162.         let passwordKey = "password"
  163.         preferences.set(pass, forKey: passwordKey)
  164.         let didSave = preferences.synchronize()
  165.         if !didSave {
  166.             print("could not save")
  167.             return false
  168.         }else{
  169.             return true
  170.         }
  171.        
  172.     }
  173.    
  174.     func signInScreen(animation:Bool){
  175.         let signIn:SigninViewController = SigninViewController()
  176.         present(signIn, animated: animation, completion: nil)
  177.     }
  178.    
  179.     func validateUser() -> Bool{
  180.         let preferences = UserDefaults.standard
  181.         let usernameKey = "username"
  182.         if preferences.object(forKey: usernameKey) == nil{
  183.             return false //new User
  184.         }else{
  185.             return true //User Exsists
  186.         }
  187.     }
  188.  
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement