Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // MyTask
- //
- // Created by HackerU on 20/03/2017.
- // Copyright © 2017 HackerU. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var userName: UITextField!
- @IBOutlet weak var userPass: UITextField!
- var myTasks=UserDefaults.standard;
- override func viewDidLoad() {
- }
- @IBAction func btnLogin(_ sender: UIButton)
- {
- if checkPass(userName: userName.text!, userPass: userPass.text!)
- {
- //pointer to our storyBoard
- let storyboard = UIStoryboard(name: "Main", bundle:nil)
- //pointer to our controller
- let controller = storyboard.instantiateViewController(withIdentifier: "taskListVC")
- //move to our new control view
- self.present(controller, animated: true, completion: nil)
- }
- else
- {
- showCustomDialog(customTitle: "Error", msg: "Wrong password")
- }
- }
- @IBAction func btnRegister(_ sender: UIButton) {
- let alert=UIAlertController(title: "Register new user", message: nil, preferredStyle: .alert)
- var uName,uPass,uPassCheck:UITextField!
- //username text field
- alert.addTextField(configurationHandler: {(input) in
- //configurations:
- input.placeholder="user name"
- uName=input //store in uName reference
- })
- //user password text field
- alert.addTextField(configurationHandler: {(input) in
- //configuration
- input.placeholder="password"
- input.isSecureTextEntry=true;
- uPass=input
- })
- //user checkPass text field
- alert.addTextField(configurationHandler: {(input) in
- //configuration
- input.placeholder="password check"
- input.isSecureTextEntry=true;
- uPassCheck=input
- })
- func okHandler(action:UIAlertAction)
- {
- //check if user exists
- if userExists(user: uName.text!)
- {
- showCustomDialog(customTitle: "Error" ,msg: "User already exists")
- return
- }
- //check if password match
- if uPass.text != uPassCheck.text
- {
- //show alert dialog with our error
- showCustomDialog(customTitle:"Error", msg: "password not match")
- return
- }
- //write new user to our plist
- //print ("user name:\(uName.text!) user pass:\(uPass.text!)")
- myTasks.setValue(uPass.text!, forKey: uName.text!)
- showCustomDialog(customTitle: "Success", msg: "Our nigger was add")
- }
- alert.addAction(UIAlertAction(title: "ok", style: .default, handler: okHandler))
- alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
- present(alert, animated: true, completion: nil)
- }
- func showCustomDialog(customTitle:String, msg:String)
- {
- let alert=UIAlertController(title: customTitle, message: msg, preferredStyle: .alert)
- //add button with empty handler (just dismiss)
- alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
- //show alert dialog to user
- show(alert, sender:nil)
- }
- func userExists(user:String)->Bool
- {
- //create a pointer to plist file
- let usersList=Bundle.main.path(forResource: "myTasks", ofType: "plist")!
- let uData=NSDictionary(contentsOfFile: usersList) //genral Dictinary
- let myUserData:[String:[String:NSObject]]=uData as! Dictionary
- return !(myUserData.index(forKey: user)==nil)
- }
- func checkPass(userName:String, userPass:String)->Bool
- {
- if !userExists(user: userName)
- {
- showCustomDialog(customTitle: "Error", msg: "user not in the system, please register")
- return false
- }
- //create a pointer to plist file
- let usersList=Bundle.main.path(forResource: "myTasks", ofType: "plist")!
- let uData=NSDictionary(contentsOfFile: usersList) //genral Dictinary
- let myUserData:[String:[String:NSObject]]=uData as! Dictionary
- return myUserData[userName]!["password"]! as! String==userPass
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement