Advertisement
stronk_8s

IOS Dynamic Login

Apr 11th, 2025
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.32 KB | Source Code | 0 0
  1.  
  2. import UIKit
  3. import CoreData
  4.  
  5. class ViewController: UIViewController {
  6.     @IBOutlet weak var username: UITextField!
  7.     @IBOutlet weak var password: UITextField!
  8.     var user : UserTbl?
  9.     var users :[UserTbl]=[]
  10.     override func viewDidLoad() {
  11.        
  12.         super.viewDidLoad()
  13.         let dirpath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true )
  14.         print(dirpath[0]
  15.     }
  16.  
  17.     @IBAction func btnLogin(_ sender: UIButton) {
  18.         let appDel = UIApplication.shared.delegate as! AppDelegate
  19.         let moc = appDel.persistentContainer.viewContext
  20.         let fr = NSFetchRequest<UserTbl>(entityName: "UserTbl")
  21.  
  22.         //fetch all user
  23.         do{
  24.             users = try moc.fetch(fr)
  25.         }
  26.         catch{
  27.             print(NSError.self)
  28.         }
  29.  
  30.         //validate all user using for loop
  31.         for user in users {
  32.             if username.text == user.username && password.text == user.password{
  33.                 navigate()
  34.             }
  35.             else{
  36.                 print("Error")
  37.             }
  38.         }
  39.        
  40.     }
  41.    
  42.     func navigate(){
  43.         let next = storyboard?.instantiateViewController(withIdentifier: "cat") as! CategoryViewController
  44.         navigationController?.pushViewController(next, animated: true)
  45.     }
  46.    
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement