Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import UIKit
  2. import Material
  3. class LoginViewController: UIViewController, UITextFieldDelegate {
  4. @IBOutlet weak var userNameTxt: TextField!
  5. @IBOutlet weak var passTxt: TextField!
  6. @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
  7.  
  8. var userArr: NSMutableArray = NSMutableArray()
  9. // var userid: Int = Int()
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. //Mark: Keybard funtion call through Notification Center
  14. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
  15. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
  16. self.hideKeyboard()
  17.  
  18. }
  19. override func viewWillAppear(_ animated: Bool) {
  20. userNameTxt.text = ""
  21. passTxt.text = ""
  22. }
  23. //Mark: Resgin keybard when return key press
  24. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  25. textField.resignFirstResponder()
  26. return true
  27. }
  28. //Mark: Resgin keybard when user tap outside from textfield
  29. func hideKeyboard(){
  30. let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
  31. view.addGestureRecognizer(tap)
  32. }
  33. func dismissKeyboard(){
  34. self.view.endEditing(true)
  35. }
  36.  
  37. @IBAction func loginBtn(_ sender: RaisedButton) {
  38.  
  39. var info: Database_Modal = Database_Modal()
  40. info.username = userNameTxt.text!
  41. info.password = passTxt.text!
  42. activityIndicator.startAnimating()
  43. if (userNameTxt.text?.isEmpty)!{
  44. displayAlertMessage(userMessage: "Please Enter Username")
  45. }else if (passTxt.text?.isEmpty)!{
  46. displayAlertMessage(userMessage: "Please Enter password")
  47. }
  48. if (info.username == userNameTxt.text) && (info.password == passTxt.text){
  49. userArr = Database_Manager.getInstance().fetchUserManagmentData(info: info)
  50. if userArr.count > 0 {
  51. info = userArr.object(at: 0) as! Database_Modal
  52. UserDefaults.standard.set(info.userId, forKey: "USER_ID")
  53. UserDefaults.standard.set(info.username, forKey: "userName")
  54. print(info.userId)
  55. let vc = self.storyboard?.instantiateViewController(withIdentifier: "HOME") as? SWRevealViewController
  56. self.present(vc!, animated: true, completion: nil)
  57. print("User Exist")
  58. }else{
  59. activityIndicator.stopAnimating()
  60. displayAlertMessage(userMessage: "Please Enter Right Credential")
  61. viewWillAppear(true)
  62. print("User Not Exist")
  63. }
  64. }
  65. }
  66. //Mark: Keyboard function to step up screen when keyboard appear
  67. func keyboardWillShow(notification: NSNotification) {
  68.  
  69. if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue) != nil {
  70. if self.view.frame.origin.y == 0{
  71. self.view.frame.origin.y -= 100
  72. }
  73. }
  74.  
  75. }
  76.  
  77. func keyboardWillHide(notification: NSNotification) {
  78. if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue) != nil {
  79. if self.view.frame.origin.y != 0{
  80. self.view.frame.origin.y += 100
  81. }
  82. }
  83.  
  84. }
  85. //Mark: Display Alert Message
  86. func displayAlertMessage(userMessage: String){
  87. let alert = UIAlertController(title: "Alert!", message: userMessage, preferredStyle: UIAlertControllerStyle.alert)
  88. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
  89.  
  90. alert.addAction(okAction)
  91. self.present(alert, animated: true, completion: nil)
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement