2

I have a login view controller that users need to type username and password in order to see the homepage. Now the users must type in the username and password every single time, which isn't convenient. I need to have a check box for "remember username" and "remember password" so that users can choose wether or not they would like to have their username of password stored the next time they login.

Any good way to do that? Or do I have to use Keychain, which according to many people is the best way?

My code is as follows:

import UIKit

import Firebase

class LoginViewController: UIViewController {

    var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView = UIImageView(frame: view.bounds)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = #imageLiteral(resourceName: "background")
        imageView.center = view.center
        view.addSubview(imageView)
        self.view.sendSubview(toBack: imageView)
        activityIndicator.hidesWhenStopped = true

    }

    //Outlets
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!


    //Login Action
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!

    @IBAction func loginAction(_ sender: AnyObject) {
        self.activityIndicator.startAnimating()

        if self.emailTextField.text == "" || self.passwordTextField.text == "" {

            //Alert to tell the user that there was an error because they didn't fill anything in the textfields because they didn't fill anything in

            let alertController = UIAlertController(title: "Error", message: "Please enter an email and password.", preferredStyle: .alert)

            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)

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

            self.activityIndicator.stopAnimating()

        } else {

            Auth.auth().signIn(withEmail: self.emailTextField.text!, password: self.passwordTextField.text!) { (user, error) in

                if error == nil {

                    //Print into the console if successfully logged in
                    print("You have successfully logged in")

                    //Go to the HomeViewController if the login is sucessful
                    let vc = self.storyboard?.instantiateViewController(withIdentifier: "Home")
                    self.present(vc!, animated: true, completion: nil)

                    self.activityIndicator.stopAnimating()

                } else {

                    //Tells the user that there is an error and then gets firebase to tell them the error
                    let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)

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

                    self.activityIndicator.stopAnimating()
                }
            }
        }

    }
}
Dharma
  • 3,007
  • 3
  • 23
  • 38
Andrew Qin
  • 67
  • 2
  • 11
  • try using Keychain its a safer way, refer https://medium.com/ios-os-x-development/securing-user-data-with-keychain-for-ios-e720e0f9a8e2 – jerfin Jul 18 '17 at 10:07

3 Answers3

4

Below are some ways in which you can stored your credential:

  1. NSUserDefault

  2. plist

  3. KeyChain Store

  4. Local Database (SQLite)

  5. CoreData

From above all, NSUserDefault and Keychain are best and easy way to use.

NSUserDefault:

For Storing into Default:

UserDefaults.standard.set(true, forKey: "Key") //Bool
UserDefaults.standard.set(1, forKey: "Key")  //Integer
UserDefaults.standard.set("TEST", forKey: "Key") //setObject

Getting from Defaults:

 UserDefaults.standard.bool(forKey: "Key")
 UserDefaults.standard.integer(forKey: "Key")
 UserDefaults.standard.string(forKey: "Key")

KeyChain: . Checkout this demo for keychain.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
1

You can use any one of the below approach -

  1. NSUserDefaults
  2. KeychainWrapper
  3. NSURLCredential

Reference URL iOS: How to store username/password within an app?

Annie Gupta
  • 2,786
  • 15
  • 23
1

Keychain is the most secure place to store the raw password.It means better than any other places.

You can use KeychainWrapper which is providing from Apple’s Keychain Services Programming Guide.

Dharma
  • 3,007
  • 3
  • 23
  • 38