lfalkau
lfalkau

Reputation: 916

Save UISwitch state in UserDefaults

I would like to save the state of a UISwitch label with UserDefaults. My code looks like this:

func viewDidAppear() {
    mySwitch.setOn(userDefaults.standard.bool(forKey: "mySwitchValue"), animated: true)
}

func viewWillDesappear() {
    UserDefaults.standard.set(mySwitch.isOn, forKey: "mySwitchValue")
}

But in the app, when I leave the switch view, and I return in, the UISwitch isn't as I turned it.

Upvotes: 3

Views: 1987

Answers (2)

Manganese
Manganese

Reputation: 690

This is not an answer to your original query, but an answer to another query in the comment. Question: How to set the default state of UISwitch as on, if application is launched for the first time? Though ideally, it should be asked as another question, given it is incremental, the code is below:

import UIKit

class ViewController: UIViewController {

    let userDefaults = UserDefaults.standard

    var firstTimeAppLaunch: Bool {
        get {
            // Will return false when the key is not set.
            return userDefaults.bool(forKey: "firstTimeAppLaunch")
        }
        set {}
    }

    @IBOutlet weak var mySwitch: UISwitch!

    @IBAction func switchAction(_ sender: UISwitch) {
        userDefaults.set(sender.isOn, forKey: "mySwitchValue")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if !firstTimeAppLaunch {
            // This will only be trigger first time the application is launched.
            userDefaults.set(true, forKey: "firstTimeAppLaunch")
            userDefaults.set(true, forKey: "mySwitchValue")
        }

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
    }

}

Note that you could do this within AppDelegate's function:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Could add the above code within this as well. Upto you. 
        return true
    }

Upvotes: 0

Manganese
Manganese

Reputation: 690

Probably, what rmaddy pointed out earlier is the issue. In that case go for spell thingy.

Otherwise, it is possible that setting the value of your switch's state when view is disappearing is not a judicious choice. As when application goes into background other processes are acted upon alongside, and probably setting default is not effected before application closes.

I would generally set such values when I am calling such functions, i.e., in the switch action. As soon as a user changes the switch state, save it in defaults, that way when you retrieve it when viewDidAppear, it will work.

import UIKit

class ViewController: UIViewController {

    let userDefaults = UserDefaults.standard

    @IBOutlet weak var mySwitch: UISwitch!

    @IBAction func switchAction(_ sender: UISwitch) {
        userDefaults.set(sender.isOn, forKey: "mySwitchValue")
    }

    override func viewDidAppear(_ animated: Bool) {
        mySwitch.isOn = userDefaults.bool(forKey: "mySwitchValue")
    }
}

Demo below:

enter image description here

Upvotes: 2

Related Questions