首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UserDefaults自动登录故障

UserDefaults自动登录故障
EN

Stack Overflow用户
提问于 2020-09-02 19:00:23
回答 1查看 192关注 0票数 0

我已经尝试了一切在阳光下允许用户保持登录通过UserDefaults。然而,每次我登录,关闭应用程序,并再次打开它,应用程序恢复到登录屏幕。我试过使用其他类似问题的解决方案,但都没有奏效。

当点击“登录”按钮时,下面是我登录屏幕上的一些代码:

代码语言:javascript
复制
@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的代码:

代码语言:javascript
复制
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())

代码语言:javascript
复制
    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:

代码语言:javascript
复制
 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()


    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-02 19:44:22

这里的问题是,UIWindow(frame:)没有像预期的那样工作,启动iOS 13时,您需要将逻辑转移到SceneDelegate.swiftUIWindow(windowScene:)。此外,请看缩进和关闭括号。

旧答案:

假设check()中的第一个check()计算结果为true,您正在执行self.window?.rootViewController = initialViewControllerself.window?.makeKeyAndVisible(),但是当check()完成时,您将创建一个新的UIWindow,并在这个新的UIWindow上执行window?.makeKeyAndVisible()

我认为你想做的是(试着保持你的造型):

代码语言:javascript
复制
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()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63711997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档