0

I am developing a mac app and I want to put my settings (array of strings) into a file. If that file exists in the same folder as the app it is read and it overwrites default settings.

No problems writing the file but when reading with code:

NSArray* settingsInput = [[NSArray alloc] initWithContentsOfFile:@"./SettingsFile"];

something strange happens. When running the app from XCode (the settings file is in the build/debug folder next to the app) settings are read without a problem. When I archive the app and run it from the desktop, the file cannot be loaded. Even when I copy the app from the build folder to the desktop it does not work.

What could be the reason for this kind of behaviour? How can I read this file?

1
  • "If that file exists in the same folder as the app". And what folder would that be? Commented Sep 19, 2013 at 9:38

3 Answers 3

3

It may be a better Idea to use the normal prefence system. NSUserDefaults.

There a couple of ways you can do it. But the idea is to give your app a set of default preference which are registered for you in the correct domain and always with a fresh app.

Using the registerDefaults: from NSUserDefaults.

See Apples documentation NSUserDefaults and its #registerDefaults

But the one I would use is :

Copy a plist file into the supporting files in you Xcode project.

enter image description here

Making sure "Copy files into destination group's folder" is checked. And the "Add to targets is check also"

enter image description here

The plist file should contain your array of strings.

enter image description here

(I created mine. By duplicating another plist in my user preferences. Renaming it. Copying it to the project. Selecting it and editing it to how I needed it. Making sure I use the file menu ->'Save' to save the changes. )

Declare a NSUserDefaults * prefs;

enter image description here

Now in the - (id)init method for the app. you register the contents of the file as your default preferences.

- (id)init
{
    self = [super init];
    if (self) {


        prefs = [NSUserDefaults standardUserDefaults] ;
        NSString *registerDefaultsPlistFile= [[NSBundle mainBundle] pathForResource:@"registerDefaults" ofType:@"plist"];
  [prefs registerDefaults:[NSDictionary dictionaryWithContentsOfFile: registerDefaultsPlistFile]];


    }
    return self;
}

You can later make a call to read these preferences.

 NSLog(@" arrayOfStrings =   %@",  [prefs objectForKey:@"arrayOfStrings" ]);

These default preferences are NOT written to file/out unless you make a change to them. By written to file I mean to the applications preference file. Once you do make a change to them then they will be written out into the users preferences and those are what will be used from then on.

Sign up to request clarification or add additional context in comments.

Comments

2

You should not rely on the current directory of the app. You should either read from the app bundle (see NSBundle class for get the correct path) or the app's document directory (see NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ...).

Comments

1

The UNIX concept of the current working directory is not commonly used in Mac desktop applications. When launching an app through the Finder it's usually set to the root directory of the boot volume.

You should find another way to determine locations for your settings files. Good spots would be ~/Library/Preferences or ~/Library/Application Support/MyApp. You can get the absolute path to these directories using:

NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomain, YES)[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.