Swift 用户默认自动登录问题
我已经尝试了sun下的一切,以允许用户通过UserDefaults保持登录状态。但是,每次我登录、关闭应用程序并再次打开它时,应用程序都会恢复到登录屏幕。我曾尝试使用其他类似问题的解决方案,但都不奏效 以下是点击“登录”按钮时我登录屏幕上的一些代码:Swift 用户默认自动登录问题,swift,xcode,nsuserdefaults,userdefaults,Swift,Xcode,Nsuserdefaults,Userdefaults,我已经尝试了sun下的一切,以允许用户通过UserDefaults保持登录状态。但是,每次我登录、关闭应用程序并再次打开它时,应用程序都会恢复到登录屏幕。我曾尝试使用其他类似问题的解决方案,但都不奏效 以下是点击“登录”按钮时我登录屏幕上的一些代码: @IBAction func logInTapped(_ sender: Any) { let email = firstNameTF.text!.trimmingCharacters(in: .whitespacesAndNewlines
@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())
指向调试项目的zip文件的链接:
新的场景场景场景:
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:)
。另外,请看缩进和结束括号
旧答案:
假设check()
中的第一个if
计算结果为true
您正在执行的self.window?.rootViewController=initialViewController
和self.window?.makeKeyAndVisible()
但当check()时
完成您正在创建一个新的UIWindow
并在这个新的UIWindow
上执行window?.makeKeyAndVisible()
我想你想做的是(尽量保持你的风格):
谢谢你的快速回复。我尝试了你的建议,但应用程序仍然无法保持登录状态。是否可能因为我正在实例化的视图控制器TBCVC是一个选项卡栏控制器而不是一个普通的视图控制器,所以代码的执行方式不一样?这是我唯一能想到的另一件事。你确定
userDefaults.value(forKey:“user\u uid\u key”)为?Bool
在运行check()
时是否为true
呢?我刚刚用打印语句确认了userDefaults.value(forKey:“user\u uid\u key”)为?当运行check()
时,Bool
为真。我不确定它可能是什么。我仍然停留在这个问题上。你能创建一个测试项目,在那里你可以复制这个吗?关于你悲伤地提供的信息@MichaelStangas,我想不出其他任何事情
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()
}
}
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()
}
}