我已经尝试了一切在阳光下允许用户保持登录通过UserDefaults。然而,每次我登录,关闭应用程序,并再次打开它,应用程序恢复到登录屏幕。我试过使用其他类似问题的解决方案,但都没有奏效。
当点击“登录”按钮时,下面是我登录屏幕上的一些代码:
@IBAction func logInTapped(_ sender: Any) {
let email = firstNameTF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = lastNameTF.text!.trimmingCharacters(in: .whitespacesAndNewlines)
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil{
//couldnt sign in
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
} else {
if #available(iOS 13.0, *) {
self.errorLabel.text = "Logged in"
self.errorLabel.isHidden = false
print("log in successful")
self.userDefaults.setValue(true, forKey: "user_uid_key")
self.userDefaults.synchronize()
self.transitionToHome()
} else {
}
}
}
}最后,下面是我的AppDelegate的代码:
var window: UIWindow?
let userDefaults = UserDefaults.standard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
check()
window = UIWindow()
window?.makeKeyAndVisible()// window?.rootViewController = UINavigationController(rootViewController: SViewController())
return true
}
func check() {
if userDefaults.value(forKey: "user_uid_key") as? Bool == true {
self.window = UIWindow(frame: UIScreen.main.bounds)链接到调试项目的zip文件:https://drive.google.com/file/d/1Hd-E4yaHN70nH2wj0l9rP6xDLUC9oHEA/view?usp=sharing
新SceneDelgeate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
if userDefaults.value(forKey: "user_uid_key") as? Bool == true {
print("userdefaults function is running")
self.window = UIWindow(windowScene: windowScene)
// self.window = UIWindow(windowScene: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TBCID") as! UITabBarController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}发布于 2020-09-02 19:44:22
这里的问题是,UIWindow(frame:)没有像预期的那样工作,启动iOS 13时,您需要将逻辑转移到SceneDelegate.swift和UIWindow(windowScene:)。此外,请看缩进和关闭括号。
旧答案:
假设check()中的第一个check()计算结果为true,您正在执行self.window?.rootViewController = initialViewController和self.window?.makeKeyAndVisible(),但是当check()完成时,您将创建一个新的UIWindow,并在这个新的UIWindow上执行window?.makeKeyAndVisible()。
我认为你想做的是(试着保持你的造型):
var window: UIWindow?
let userDefaults = UserDefaults.standard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
check()
return true
}
func check() -> Bool {
if userDefaults.value(forKey: "user_uid_key") as? Bool == true {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TBCID") as! TBCVC
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
} else {
window = UIWindow()
window?.rootViewController = UINavigationController(rootViewController: SViewController())
window?.makeKeyAndVisible()
}
}https://stackoverflow.com/questions/63711997
复制相似问题