-1

My question is similar to this one: How to detect first time app launch on an iPhone

I do not completely understand the answer and my problem is slightly different (Int not Bool) hence why I'm asking this question.

How do I set some default values for an app, lets say I want variable a = 10 and b = 20. These would be the defaults set the very first time you downloaded and open the app, but the user would have a choice to change them. Once the settings are changed they need to stay like that, it would be very annoying for the settings to revert to default everytime you open the app. I know how to use NSUserDefault to save the changes made by the user so that the next time they open the app their settings are used. However I don't know how to set initial values that will only be set the very first time the app is opened.

Community
  • 1
  • 1
Jack Hayton
  • 363
  • 4
  • 18

2 Answers2

1

However I don't know how to set initial values that will only be set the very first time the app is opened.

That is what registerDefaults: is for.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I have looked at the question you have linked, and searched registerDefaults in Xcode documentation, but because I'm very new to coding I still don't understand how to implement it. Could you show me what code I need to write to set a variable x = 10? – Jack Hayton Aug 13 '15 at 18:56
  • A _variable_? If you set a variable in your code, it will be set every time the app runs. This isn't about a _variable_. This is about a one-time value to be retrieved from NSUserDefaults. Once you've registered a default (with `registerDefaults:`), then if you've never set that default, the value when you fetch that default (`objectForKey:`) is the registered default - it is the "default default". Once you've set that default (`setObject:forKey:`), the fetched value (`objectForKey:`) is the most recently set value. – matt Aug 13 '15 at 19:12
  • Im new to this, sorry if my questions are unclear. My problem is as follows. I have 5 values that I need to set, so the first time it is launched they have my default value. The user can then go and change them and they will remain this way unless they are changed again. I know that I need to use NSUserDefaults to set this up. From your answer I know that I need to use registerDefaults, the problem is that i have no clue how to do that, I don't know what code to write, I have looked everywhere and can't understand what to write. Could you show exactly what the entire code looks like? – Jack Hayton Aug 13 '15 at 19:21
  • http://www.apeth.com/iOSBook/ch36.html#_user_defaults – matt Aug 13 '15 at 19:30
1

You use the registerDefaults() method to do this. Anything you pass to registerDefaults will be used as a "setting of last resort", and checked only if the user hasn't set something else for that key. Everything passed to registerDefaults isn't saved to disk, so you just call it each time your program starts up, and there's no need to check if it's the first time or anything like that.

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • I still don't quite understand, why would the registerDefaults() not change the values the second time the user opens the app, or third.. Is it because when the user changes the default settings and those are saved to NSUserDefaults, then this counts as a "change" and so the registerDefaults() does not go through? Please explain a bit more I'm still confused. – Jack Hayton Aug 13 '15 at 18:36
  • "why would the registerDefaults() not change the values the second time the user opens the app, or third" Because it doesn't. It is there to do exactly what you want to do. "Is it because when the user changes the default settings and those are saved to NSUserDefaults, then this counts as a "change" and so the registerDefaults() does not go through?" _Yes_, exactly. _Try it and see_. – matt Aug 13 '15 at 19:14
  • NSUserDefaults is not a single key-value store, it's a list of key-value stores. Setting a value modifies one entry in the list (and stores it to disk). Registering a value modifies a different, later entry in the list. So setting and registering don't interfere with each other, but set values always take priority over registered values. – Catfish_Man Aug 13 '15 at 22:28
  • Thanks for the clarification :) – Jack Hayton Aug 14 '15 at 01:02