如何解决UserDefaults自动登录问题
我已经尝试过在阳光下进行所有操作,以允许用户通过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
New 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()
}
}
解决方法
这里的问题是UIWindow(frame:)
不能按预期工作,从iOS 13开始,您需要将逻辑移至SceneDelegate.swift
和UIWindow(windowScene:)
。另外,请查看缩进和右括号。
旧答案:
假设if
中的第一个check()
评估为true
,您正在做self.window?.rootViewController = initialViewController
和self.window?.makeKeyAndVisible()
,但是当check()
完成时,您正在创建新的UIWindow
并在此新的window?.makeKeyAndVisible()
上进行UIWindow
。
我认为您想要做的是(尝试保持样式):
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()
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。