2017-07-18 152 views
2

我有一個登錄視圖控制器,用戶需要鍵入用戶名和密碼才能看到主頁。現在用戶必須每次輸入用戶名和密碼,這是不方便的。我需要有一個「記住用戶名」和「記住密碼」的複選框,以便用戶可以選擇是否希望他們下次登錄時存儲密碼的用戶名。Xcode:存儲用戶名和密碼

任何好的方法來做到這一點?或者我必須使用鑰匙扣,這是很多人認爲最好的方法嗎?

我的代碼如下:

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

    } 
} 
+0

嘗試使用鑰匙串其更安全的方式,請參閱https://medium.com/ios-os-x-development/securing-user-data-with-keychain-for-ios-e720e0f9a8e2 – Arun

回答

5

下面是一些方法,可以在其中存儲您的憑據:

  1. NSUserDefault

  2. 的plist

  3. 鑰匙鏈商店

  4. 本地數據庫(SQLite的)

  5. CoreData

從上面所有,NSUserDefault和鑰匙扣都是用最好的,簡單的方法。

NSUserDefault:

存儲到默認值:

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

從默認獲取:

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

鑰匙鏈:Checkout this demo for keychain.

+1

最佳答案是這一個,多個選項。 – ppinho

+0

至於NSUserDafault,你如何在我的代碼中實現代碼?特別是在我需要用戶界面需要複選框的情況下。 –

+0

當您在api響應中獲得成功時,您需要將電子郵件地址和密碼保存爲默認值:UserDefaults.standard.set(txtEmail.text,forKey:「email_address」)。 – Nirmalsinh

相關問題