问题:在应用终止时保持登录状态。用户在应用程序最小化时登录,但是当我终止应用程序时,用户不会保持登录状态。任何人都可以看到问题吗?
- 我不希望每次打开应用程序时都要求用户注册
ViewController.swift (" TEST2"永远不会被执行)
import UIKit
import Firebase
import FirebaseDatabase
import KeychainSwift
class ViewController: UIViewController {
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var passwordField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
print("TEST1")
let keyChain = DataService().keyChain
if keyChain.get("uid") != nil {
performSegue(withIdentifier: "SignIn", sender: nil)
print("TEST2")
}
}
func CompleteSignIn(id: String){
let keyChain = DataService().keyChain
keyChain.set(id , forKey: "uid")
}
@IBAction func SignIn(_ sender: Any){
if let email = emailField.text, let password = passwordField.text {
FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
if error == nil {
self.CompleteSignIn(id: user!.uid)
self.performSegue(withIdentifier: "SignIn", sender: nil)
} else {
FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in
if error != nil {
print("cant sign in user")
} else {
self.CompleteSignIn(id: user!.uid)
self.performSegue(withIdentifier: "SignIn", sender: nil)
}
}
}
}
}
}
}
DataService.swift
import Foundation
import Firebase
import FirebaseDatabase
import KeychainSwift
let DB_BASE = FIRDatabase.database().reference()
class DataService {
private var _keyChain = KeychainSwift()
private var _refDatabase = DB_BASE
var keyChain: KeychainSwift {
get {
return _keyChain
} set {
_keyChain = newValue
}
}
}