How to save on/off value of a switch with User defaults?

3k views Asked by At

I have created a custom table view cell. In my app, there are two files. I have a separate file for the custom table view cell. Then I have a tableviewcontroller file for the table as a whole.

On my custom table view cell, there is a UISwitch. The initial value of this UISwitch is off. In the file for the custom table view cell, I have created a function for the switch. I have also created an outlet for the switch.

@IBOutlet var theSwitch: UISwitch!

@IBAction func mySwitch(_ sender: AnyObject) {
}

In my table view controller, I have already created the view did appear function. I am assuming what I need to do is save the value of the switch in

@IBAction func mySwitch(_ sender: AnyObject) {
}

Then in the table view controller, somehow call it in this view did appear function.

override func viewDidAppear(_ animated: Bool) {
}

What I am having trouble with is saving the value of the switch if it is changed. For example, if a user opens the app and turns the switch on, I want the app to save it, so that when the app is reopened, the value of the switch is still on. Hopefully, this can be done by using User Defaults. If someone can show me the code to do this, that would be great.

4

There are 4 answers

2
PiyushRathi On

You can check value like this:

Swift 2

  let userDefaults = NSUserDefaults.standardUserDefaults()
  if userDefaults.valueForKey("SwitchValue") != nil{

        let object = userDefaults.valueForKey("SwitchValue") as? NSNumber
        yourSwitch.on = (object?.boolValue)!
    }

and for saving value to userDefaults:

userDefault.setValue(NSNumber(bool: yourSwitch.value), forKey:"SwitchValue")

Swift 3

let uDefault = UserDefaults.standard

if uDefault.value(forKey: "SwitchValue") != nil{
     let object = uDefault .value(forKey: "SwitchValue") as? NSNumber
     yourSwitch.isOn = (object?.boolValue)!
}

and set value

uDefault.set(NSNumber(bool: yourSwitch.value), forKey: "SwitchValue")

hope this helps

1
jignesh Vadadoriya On

In your IBAction method you an save the value like this way

@IBAction func mySwitch(_ sender: UISwitch) {

    UserDefaults.standard.set(sender.isOn, forKey: "Switch")

}

In your table view delegate method write this way to get the value from user default and set the status of you switch as per your previous value

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let yourCell = tableView.dequeueReusableCellWithIdentifier("ProfileCell", forIndexPath: indexPath) as! MenuProfileTableViewCell

    yourCell.yourSwitch.setOn(UserDefaults.standard.bool(forKey: "Switch"), animated: true)
}
2
Sunil Chauhan On

As PiyushRathi said, you can do it with NSNumber, but you can do it via setting to Bool as well:

Swift 2.3

//  saving switch value
 NSUserDefaults.standardUserDefaults().setBool(theSwitch.on, forKey: "SwitchValue")

//  getting back the value
theSwitch.setOn(NSUserDefaults.standardUserDefaults().boolForKey("SwitchValue"), animated: true)

For Swift 3 (As EmilioPelaez suggested):

//  saving switch value
UserDefaults.standard.set(theSwitch.isOn, forKey: "SwitchValue")

//  getting back the value
theSwitch.setOn(UserDefaults.standard.bool(forKey: "SwitchValue"), animated: true)

Also, if you have many UI components to save other than switch, you should also have a look at this UIViewController's "State Preserving Guide".

0
Arun K On

You can use setBoolForkey in swift 3 like this

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

where you want to save and you can check the value in viewdidload() to change the status of switch according to the value of "onoroff"

 theSwitch.setOn(UserDefaults.standard.bool(forKey: "onoroff"), animated: false)