Xcode:存储用户名和密码

And*_*Qin 2 xcode ios

我有一个登录视图控制器,用户需要输入用户名和密码才能看到主页.现在用户必须每次都输入用户名和密码,这不方便.我需要一个"记住用户名"和"记住密码"的复选框,以便用户可以选择是否愿意在下次登录时存储他们的密码用户名.

有什么好方法吗?或者我是否必须使用Keychain,根据很多人的说法是最好的方式?

我的代码如下:

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()
                }
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Nir*_*inh 6

以下是您可以存储凭证的一些方法:

  1. NSUserDefault

  2. plist中

  3. KeyChain商店

  4. 本地数据库(SQLite)

  5. CoreData

最重要的是,NSUserDefault和Keychain是最好用的方法.

NSUserDefault:

存储到默认值:

UserDefaults.standard.set(true, forKey: "Key") //Bool
UserDefaults.standard.set(1, forKey: "Key")  //Integer
UserDefaults.standard.set("TEST", forKey: "Key") //setObject
Run Code Online (Sandbox Code Playgroud)

从违约中获取:

 UserDefaults.standard.bool(forKey: "Key")
 UserDefaults.standard.integer(forKey: "Key")
 UserDefaults.standard.string(forKey: "Key")
Run Code Online (Sandbox Code Playgroud)

KeyChain : . 查看此演示以获取钥匙串.