我需要幫助使用我的代碼。這裏是鏈接到我的loginviewcontroller的代碼,以及我在互聯網上無法找到的任何地方解釋這一點。我試圖讓用戶第一次登錄時,他們會一直登錄,直到他們退出爲止,這樣用戶就不必每次打開應用程序時都繼續登錄。我試過了鑰匙圈和所有這些,但我無法弄清楚。我錯過了什麼?我擺脫了試圖保存信息的垃圾代碼。自動登錄Firebase用戶
import UIKit
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
class LoginViewController: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var pwField: UITextField!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if FIRAuth.auth()?.currentUser == nil {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login") as! UsersViewController
present(vc, animated: true, completion: nil)
}
}
@IBAction func loginPressed(_ sender: Any) {
guard emailField.text != "", pwField.text != "" else {return}
FIRAuth.auth()?.signIn(withEmail: emailField.text!, password: pwField.text!, completion: { (user, error) in
if let error = error {
print(error.localizedDescription)
}
if user != nil {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "centralvc")
self.present(vc, animated: true, completion: nil)
}
})
}
}