0

My app only has a password field in plain text. However, I need users to be able to open the app after the first time and not have to put in the password every time, only the first time. I don't want to use keychain because the password is not put in by the owner of the device but someone else. Here is my code for the password page, if that matters:

    import UIKit

class LoginVC: UIViewController {

    @IBOutlet weak var textpassword: UITextField!
    
    
    @IBAction func btnActionLogin(_ sender: Any) {
        UserDefaults.standard.set(true, forKey: "status")
        
        if textpassword.text == "KEY" {
            runbutton()

        }

        else {
            alertController.addAction(okAction)
            alertController.addAction(cancelAction)

            self.present(alertController, animated: true, completion: nil)

            
        }
    }
    
    // Create the alert controller
    let alertController = UIAlertController(title: "Password", message: "Please contact Admin for access to the App", preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) {
        UIAlertAction in
        NSLog("OK Pressed")
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }
    
    
    func runbutton() {
            let tabBarViewController = UIStoryboard(name: Constants.Storyboard.mainStoryBoard, bundle: nil).instantiateViewController(withIdentifier: Constants.Storyboard.tabBarController) as! UITabBarController

              view.window?.rootViewController = tabBarViewController
              view.window?.makeKeyAndVisible()

    }
    
     func touchBegin(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }

    
    struct Constants {
        struct Storyboard {

        static let homeViewController = "loginvc"
        static let tabBarController = "TabBarVC"
        static let mainStoryBoard = "Main"
            
        }
    }
    
}
Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46

1 Answers1

0

Why don't you use UserDefaults to put the password in there?

How can I use UserDefaults in Swift?

I don't know how secure your app or the password has to be but it could be an appropriate way. Once written to the Defaults you could check if the password is written there and the app wouldn't ask for a password again.

Jan-Hendrik
  • 3
  • 1
  • 7
  • would this work if you fully close the app? I'm very new to developing and need to do some minor adjustments to past apps with a few problems... – thrownunderthebus Sep 03 '20 at 21:31
  • Yes. Data in UserDefaults is stored as long as you don't overwrite them or delete the app from the device. – Jan-Hendrik Sep 04 '20 at 09:35