Ios Xcode:存储用户名和密码
我有一个登录视图控制器,用户需要键入用户名和密码才能看到主页。现在用户每次都必须输入用户名和密码,这很不方便。我需要有一个“记住用户名”和“记住密码”的复选框,以便用户可以选择是否希望在下次登录时存储密码用户名 有什么好办法吗?或者我必须使用钥匙链,根据许多人的说法,这是最好的方法 我的代码如下:Ios Xcode:存储用户名和密码,ios,xcode,Ios,Xcode,我有一个登录视图控制器,用户需要键入用户名和密码才能看到主页。现在用户每次都必须输入用户名和密码,这很不方便。我需要有一个“记住用户名”和“记住密码”的复选框,以便用户可以选择是否希望在下次登录时存储密码用户名 有什么好办法吗?或者我必须使用钥匙链,根据许多人的说法,这是最好的方法 我的代码如下: import UIKit import Firebase class LoginViewController: UIViewController { var imageView: UII
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()
}
}
}
}
}
您可以使用以下任何一种方法-
参考URL以下是一些存储凭证的方法:
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")
钥匙链: 钥匙链是存储原始密码最安全的地方。这意味着它比其他任何地方都好
您可以使用从提供的密码。尝试使用钥匙链这是一种更安全的方法,最好的答案是这一个,多个选项。至于NSUserDafault,您如何将代码实现到我的代码中?特别是在我的例子中,UI需要复选框。当api响应成功时,需要将电子邮件地址和密码存储到default.:UserDefaults.standard.set(txtEmail.text,forKey:“email\u address”)中。