我有一个登录视图控制器,用户需要输入用户名和密码才能看到主页。现在用户必须每次都输入用户名和密码,这不方便。我需要一个&#34的复选框;记住用户名"和#34;记住密码"因此,用户可以选择是否愿意在下次登录时存储他们的密码用户名。
有什么好办法吗?或者我是否必须使用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()
}
}
}
}
}
答案 0 :(得分:6)
以下是存储凭据的一些方法:
NSUserDefault
的plist
KeyChain Store
本地数据库(SQLite)
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
从默认值中获取:
UserDefaults.standard.bool(forKey: "Key")
UserDefaults.standard.integer(forKey: "Key")
UserDefaults.standard.string(forKey: "Key")
KeyChain:。 Checkout this demo for keychain.
答案 1 :(得分:1)
您可以使用以下任何一种方法 -
答案 2 :(得分:1)
Keychain 是存储原始密码最安全的地方。这意味着比其他任何地方都要好。
您可以使用KeychainWrapper提供的Apple’s Keychain Services Programming Guide。