Advertisement
Guest User

begging of myTasks

a guest
Mar 21st, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // MyTask
  4. //
  5. // Created by HackerU on 20/03/2017.
  6. // Copyright © 2017 HackerU. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. @IBOutlet weak var userName: UITextField!
  14. @IBOutlet weak var userPass: UITextField!
  15. var myTasks=UserDefaults.standard;
  16.  
  17. override func viewDidLoad() {
  18.  
  19. }
  20.  
  21. @IBAction func btnLogin(_ sender: UIButton)
  22. {
  23. if checkPass(userName: userName.text!, userPass: userPass.text!)
  24. {
  25. //pointer to our storyBoard
  26. let storyboard = UIStoryboard(name: "Main", bundle:nil)
  27. //pointer to our controller
  28. let controller = storyboard.instantiateViewController(withIdentifier: "taskListVC")
  29. //move to our new control view
  30. self.present(controller, animated: true, completion: nil)
  31. }
  32. else
  33. {
  34. showCustomDialog(customTitle: "Error", msg: "Wrong password")
  35. }
  36. }
  37.  
  38. @IBAction func btnRegister(_ sender: UIButton) {
  39. let alert=UIAlertController(title: "Register new user", message: nil, preferredStyle: .alert)
  40. var uName,uPass,uPassCheck:UITextField!
  41. //username text field
  42. alert.addTextField(configurationHandler: {(input) in
  43. //configurations:
  44. input.placeholder="user name"
  45. uName=input //store in uName reference
  46. })
  47. //user password text field
  48. alert.addTextField(configurationHandler: {(input) in
  49. //configuration
  50. input.placeholder="password"
  51. input.isSecureTextEntry=true;
  52. uPass=input
  53. })
  54.  
  55. //user checkPass text field
  56. alert.addTextField(configurationHandler: {(input) in
  57. //configuration
  58. input.placeholder="password check"
  59. input.isSecureTextEntry=true;
  60. uPassCheck=input
  61. })
  62.  
  63.  
  64. func okHandler(action:UIAlertAction)
  65. {
  66. //check if user exists
  67. if userExists(user: uName.text!)
  68. {
  69. showCustomDialog(customTitle: "Error" ,msg: "User already exists")
  70. return
  71. }
  72.  
  73.  
  74. //check if password match
  75. if uPass.text != uPassCheck.text
  76. {
  77. //show alert dialog with our error
  78. showCustomDialog(customTitle:"Error", msg: "password not match")
  79. return
  80. }
  81. //write new user to our plist
  82. //print ("user name:\(uName.text!) user pass:\(uPass.text!)")
  83. myTasks.setValue(uPass.text!, forKey: uName.text!)
  84. showCustomDialog(customTitle: "Success", msg: "Our nigger was add")
  85. }
  86. alert.addAction(UIAlertAction(title: "ok", style: .default, handler: okHandler))
  87. alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
  88. present(alert, animated: true, completion: nil)
  89.  
  90. }
  91.  
  92. func showCustomDialog(customTitle:String, msg:String)
  93. {
  94. let alert=UIAlertController(title: customTitle, message: msg, preferredStyle: .alert)
  95. //add button with empty handler (just dismiss)
  96. alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
  97. //show alert dialog to user
  98. show(alert, sender:nil)
  99. }
  100.  
  101. func userExists(user:String)->Bool
  102. {
  103. //create a pointer to plist file
  104. let usersList=Bundle.main.path(forResource: "myTasks", ofType: "plist")!
  105.  
  106. let uData=NSDictionary(contentsOfFile: usersList) //genral Dictinary
  107.  
  108. let myUserData:[String:[String:NSObject]]=uData as! Dictionary
  109.  
  110. return !(myUserData.index(forKey: user)==nil)
  111. }
  112.  
  113. func checkPass(userName:String, userPass:String)->Bool
  114. {
  115. if !userExists(user: userName)
  116. {
  117. showCustomDialog(customTitle: "Error", msg: "user not in the system, please register")
  118. return false
  119. }
  120. //create a pointer to plist file
  121. let usersList=Bundle.main.path(forResource: "myTasks", ofType: "plist")!
  122.  
  123. let uData=NSDictionary(contentsOfFile: usersList) //genral Dictinary
  124.  
  125. let myUserData:[String:[String:NSObject]]=uData as! Dictionary
  126.  
  127. return myUserData[userName]!["password"]! as! String==userPass
  128.  
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement