2

I'm new in IOS. Problaby i'm missing some concept. I've followed this tutorial for creating a bundle setting page.

The settings page is created and I can see 4 settings that i've created in the settings file... ok.

I'm trying to get this values when app launch with this code when app didfinishlaunchwithoptions:

//Settings:
let standardUserDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults()
print(standardUserDefaults.boolForKey("Push"))
print(standardUserDefaults.stringForKey("Idioma"))

I don't get the right configuration values... Push is enabled but I always get false, and nil in Idioma...

Update: I followed this instructions I've generated a plist file only to store defaults values and know I get the defaults values but I continue missing some concept because I want to know if any default setting is changed outside my app in the settings app of the Device. I've tested to uncheck Push value in settings of device and I still get the default value in my app... How my app knows when a default value has been changed in settings?

var myDict: NSDictionary?
    if let path = NSBundle.mainBundle().pathForResource("SettingsDefaults", ofType: "plist") {
        myDict = NSDictionary(contentsOfFile: path)
    }
    if let dict = myDict {
        print(dict["Push"])
        NSUserDefaults.standardUserDefaults().registerDefaults(dict as! [String : AnyObject])
        NSUserDefaults.standardUserDefaults().synchronize()
    }
Community
  • 1
  • 1

2 Answers2

1

It works as expected for me, Please ensure you set values prior to call.

    let standardUserDefaults : NSUserDefaults = NSUserDefaults.standardUserDefaults()

    standardUserDefaults.setBool(true, forKey: "Push")
    standardUserDefaults.setValue("Idioma Value", forKey: "Idioma")

    print(standardUserDefaults.boolForKey("Push"))
    print(standardUserDefaults.stringForKey("Idioma"))

Output

true
Optional("Idioma Value")

Even in second run if I commented the value settings it works fine.

Naveen Shan
  • 9,192
  • 3
  • 29
  • 43
0

My update note was not correct. RegisterDefaults and Synchronize works ok I was not checking correctly the value. I'm able to retrieve the changed value outside the app... Solved!