views:

57

answers:

3

Hi,

I want to store the content of some variables in a file. The user nor the application should change the value. It would be great if the user cannot read the content of this file. On startup the application reads the file and initialize some (global?) variables with the content.

The file should contain some values like server URL and so on. This could change, but I only want to manage a preference file rather than updating the source code. On an update for example only the preference file will get exchanged.

How can I manage that?

NSUserDefaults is not intended for such issues I think. Should I use a a plist or a normal txt file?

How would the access to the content of the file look like?

Cheers

+1  A: 

Sounds to me like NSUserDefaults is exactly what you need. It will let you store URLs (as strings) and other basic types of variables.

Why do you think NSUserDefaults is not the right solution here?

Give it a try! It's easy to use and reliable.

Toastor
Because everywhere it is stated as user preference. I don't need a user preference. I need a system preference. But I think you're right. One thing I didn't find out yet: How can I create a NSUserDefaults "file"? Can I specify a certain plist as NSUSerDefaults, which I created before?
testing
I thought I can simple edit the NSUserDefaults like a plist in Xcode. But it isn't that simple. You have to work with the command line tool ... You have to write "defaults write <domain> <key>". But what is the domain? The domain is the bundle identifier. How do I get the bundle identifier? I only have something like com.yourcompany.${PRODUCT_NAME:rfc1034identifier}. So I have to change the identifier. What do I type for product name? What for rfc1034identifier? Why can't that read out my project name? Questions over questions ... Info: http://www.cocoadev.com/index.pl?WorkingWithUserDefaults
testing
A: 

Use a plist. You can load it with -[NSDictionary initWithContentsOfFile:] (you can save a dictionary to a plist just as easily with -[NSDictionary writeToFile:atomically:], though it doesn't sound like you need to do that).

Brian
I also thought about a plist. The problem is that I want to access the values from different classes. This means that either I alloc the plist every time or I write a wrapper class which I have to include in every class. So plists wouldn't the best choice.
testing
It wouldn't be that different than using NSUserDefaults (which is basically a wrapper around a plist - you can see the file it creates under `Library/Preferences/{bundleID}.plist`). And as far as including settings with the app bundle, plist is the standard.
Brian
Thanks for the info. I'll use plist and NSUserDefaults.
testing
A: 

So the solution I'll use is that I load a plist file as default value for my NSUserDefaults:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
[[NSUserDefaults standardUserDefaults] registerDefaults:settingsDict];
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];

NSString *serverURL = [settings stringForKey:@"ServerURL"];
NSLog(@"%@", serverURL);

Taken from http://stackoverflow.com/questions/789397/iphone-app-where-do-i-put-a-config-file

So I'll use both plists and NSUserDefaults. settings I'll define as global variable in the main.m. One problem remains:

How to differentiate between user defaults and system defaults?

testing