如何使用UserDefaults快速实现登录/注销导航? [英] How I can implement Login / Logout Navigation using UserDefaults in swift?

查看:100
本文介绍了如何使用UserDefaults快速实现登录/注销导航?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

** **每次运行应用程序时,我都必须先登录才能进入应用程序的主页.

****Every time when I run the app I have to do the login to enter the home page of the app.

点击登录按钮直到退出按钮时,如何存储会话.

How can I store the session when I tap the login button untill I Tap the logout button.

因此,我可以在每次运行我的应用程序时避免登录.

So, that I can avoid the login everytime when I run my application.

请帮帮我……

其他解决方案不起作用.

Other solutions are not working.

class LoginViewController: UIViewController {

    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var loginName: UITextField!

   @IBAction func submitButton(_ sender: Any) {

  UserDefaults.standard.set(true, forKey: "isLoggedIn")

        if loginName.text == "test" && password.text == "test" {

            UserDefaults.standard.set(true, forKey: "status")
            Switcher.updateRootVC()

        }
        else{
            print("Invalid credentials")
        }
    }
}

class ProfileViewController: UIViewController {

@IBAction func submitButton(_ sender: Any) {

    UserDefaults.standard.set(false, forKey: "status")
    Switcher.updateRootVC()

    }

}

class AppDelegate: UIResponder, UIApplicationDelegate {


  var window: UIWindow?

    func loadBaseController() {
       let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
       guard let window = self.window else { return }
       window.makeKeyAndVisible()
       if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
           let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
           self.window?.rootViewController = loginVC
       } else {
           let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
           let navigationHomeVC = UINavigationController(rootViewController: homeVC)
           self.window?.rootViewController = navigationHomeVC
       }
      self.window?.makeKeyAndVisible()
    }


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

       self.loadBaseController()

        return true

    }
}

我引用了此链接的相同方法 https: //medium.com/@paul.allies/ios-swift4-login-logout-branching-4cdbc1f51e2c ,但它没有用,因为我没有得到预期的结果.
我是正确的状态码,但不是正确的视图控制器. 请帮我解决这个问题........

I referred the same method of this link https://medium.com/@paul.allies/ios-swift4-login-logout-branching-4cdbc1f51e2c but it is not useful because I'm getting not the expected result .
I'm the correct status code but not the correct view controller. Please help me out to solve this problem........

在给定的链接中,它会将TabbarVc添加到Tabor

In the given link it is adding the TabbarVc to the tabor

推荐答案

在Xcode 11中,当我们创建一个新项目时有2个文件,因此,如果您想从委托文件中更改rootViewController,则需要从 SceneDelegate.swift 文件.

In Xcode 11 there are 2 file while we create a new project so if you want to change rootViewController from the delegate file then you need to load that controller from the SceneDelegate.swift file.

//SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window = UIWindow(frame: windowScene.coordinateSpace.bounds)
    window?.windowScene = windowScene

    self.loadBaseController()
}


func loadBaseController() {
   let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
   guard let window = self.window else { return }
   window.makeKeyAndVisible()
   if UserDefaults.standard.bool(forKey: "isLoggedIn") == false {
       let loginVC: ViewController = storyboard.instantiateViewController(withIdentifier: "login") as! ViewController
       self.window?.rootViewController = loginVC
   } else {
       let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "showData") as! HomeViewController
       let navigationHomeVC = UINavigationController(rootViewController: homeVC)
       self.window?.rootViewController = navigationHomeVC
   }
    self.window?.makeKeyAndVisible()
}

这篇关于如何使用UserDefaults快速实现登录/注销导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆