Setting default value in NSUserDefaults ios8 using swift

How to enable google toggle?

Also I read that if the user launches the app for the first time, the default will not be used. How can I check this?

In a set of settings

In settings bundle

In application settings

In settings app

In AppDelegate

let defaults:NSUserDefaults = NSUserDefaults.standardUserDefaults();
let showEnabledString:Bool = defaults.boolForKey("enabled_string");
println(showEnabledString); // true

 let appDefaults: NSDictionary = NSDictionary(object: "YES", forKey: "enabled_string");

      

// This code also does not change the appearance in the settings app

NSUserDefaults.standardUserDefaults().registerDefaults(appDefaults as [NSObject : AnyObject]);
           NSUserDefaults.standardUserDefaults().synchronize();

      

+3


source to share


1 answer


if NSUserDefaults.standardUserDefaults().valueForKey("enabled_string") as? Bool != true {
   yourSwitchName.setOn(false, animated: false)
}else{
   yourSwitchName.setOn(true, animated: false)
}

      



Place the above in viewDidLoad

or viewDidAppear

. This is how I set my switches at least.

0


source







All Articles