从其他viewControllers访问Swift中的UserDefaults

时间:2017-03-05 16:34:25

标签: ios swift uiviewcontroller nsuserdefaults userdefaults

在我的应用程序中,我使用UserDefaults存储用户的登录状态(无论他们是否登录)及其用户名。它工作正常,当我登录时,关闭应用程序,然后再次打开它我的应用程序跳过登录页面并认识到我已经登录。虽然,我现在正在尝试将登出按钮安装到单独的viewController。单击时,此注销按钮需要1.)将UserDefaults.loginStatus重置为" False" 2.)将UserDefaults.username重置为nil 3.)对登录页面执行segue。

以下是我的ViewController.swift文件中的相关代码。这是第一个控制loginPage的viewController。

import UIKit
import Firebase

let defaults = UserDefaults.standard

class ViewController: UIViewController {
func DoLogin(username: String, password: String) {
    //I Am not including a lot of the other stuff that takes place in this function, only the part that involves the defaults global variable
    defaults.setValue(username, forKey: "username")
    defaults.setValue("true", forKey: "loginStatus")
    defaults.synchronize()
    self.performSegue(withIdentifier: "loginToMain", sender: self) //This takes them to the main page of the app
}

override func viewDidLoad() {
    super.viewDidLoad()
    if let stringOne = defaults.string(forKey: "loginStatus") {
        if stringOne == "true" { //If the user is logged in, proceed to main screen
            DispatchQueue.main.async
                {
                    self.performSegue(withIdentifier: "loginToMain", sender: self)
            }
        }
    }

}

下面是我在SecondViewController.swift中的代码,特别是注销函数。

import UIKit
import Firebase

class SecondViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
     if let username = defaults.string(forKey: "username") {
            checkAppSetup(username: username) //This is an unrelated function
            //I included this because this works fine. Proving that I am able to read the defaults variable fine from this other viewController
      }
}
@IBAction func logout(_ sender: Any) {
        defaults.setValue("false", forKey: "username")
        defaults.setValue("false", forKey: "loginStatus")
        defaults.synchronize()

        performSegue(withIdentifier: "logoutSegue", sender: nil)
    }

运行注销功能时,segue运行正常但默认值不会更改。有人可以解释为什么以及我能做些什么来解决这个问题?

**旁注,我实际上并没有将默认值设置为" false"和"假"。在调试此问题时,这只是暂时的。

2 个答案:

答案 0 :(得分:2)

有几件事。

您应该使用set(_:forKey:)object(_:forKey)来读取和写入默认值的键/值对,而不是setValue(_:forKey)。 (但是,您对defaults.string(forKey: "loginStatus")的使用是正确的。)

你应该写一个nil到userName键:

defaults.set(nil, forKey: "username")

您的退出IBAction几乎肯定会将loginStatus设为false,而不是true

尝试改变这些事情。

此外,除非您在Xcode中终止应用程序而不是按设备/模拟器上的主页按钮以使其正常退出,否则没有理由调用同步。

答案 1 :(得分:2)

嘿,我最近使用了完全相同的概念:

1)在初始视图中,在viewDidLoad()中,检查某人是否已经登录,并且一次只能有一个用户登录一个设备,所以我们检查

let defaults = UserDefaults.standard
if defaults.object(forKey: "userName") != nil && defaults.object(forKey: "userPassword") != nil
   {
          let loginObject = self.storyboard?.instantiateViewController(withIdentifier: "YourSecondViewController") as! YourSecondViewController
//As someone's details are already saved so we auto-login and move to second view    
}}

2)在您的登录按钮功能中,检查您要检查的条件,然后在相同条件下,如果条件满足,则将数据保存到userDefaults。

// If no details are saved in defaults, then control will come to this part, where we will save the entered userName and Password

    let defaults = UserDefaults.standard
    defaults.set(self.enteredUseName, forKey: "userName")
    defaults.set(self.enteredPassword, forKey: "Password")
    defaults.synchronize()

3)在注销按钮上,删除userDefaults并再次加载登录视图:

let defaults = UserDefaults.standard
defaults.removeObject(forKey: "userName") //We Will delete the userDefaults
defaults.removeObject(forKey: "userPassword")
defaults.synchronize() //Sync. the defaults.
navigationController?.popToRootViewController(animated: true) //Move Back to initial view.

4)如果您正在使用导航控件,那么您必须使用:P然后您肯定会看到后退按钮,如果单击该按钮将打开第二个视图,因为您可以隐藏{{1}中的导航栏您的登录视图

viewDidLoad()
相关问题