Alex Stone
Alex Stone

Reputation: 47344

iPhone 4 iOS5 how to include a text configuration file with the app?

I know how to read/write to local files on iOS using file handles. I can dynamically create and read/write form them. What I'm trying to do right now is to include a file, about 200 lines long with the app bundle, which I hope to parse and extract configuration settings from. I really do not want to have to create an array of 200 numbers by hand.

How would I go about including a text file with the app, and making it accessible from my application?

Thank you!

Upvotes: 2

Views: 767

Answers (2)

Jef
Jef

Reputation: 2134

Why don't you create a dictionary with Property List Editor and load it like

NSString *configurationPath = [[NSBundle mainBundle] pathForResource: @"configuration" ofType: @"plist"];
NSDictionary *configuration = [NSDictionary dictionaryWithContentsOfFile: configurationPath];

So you can access its settings with

[configuration objectForKey: @"someSetting"];

If you want to write, I recommend registering that dictionary with NSUserDefaults so you could write to a copy like

[[NSUserDefaults standardUserDefaults] setObject: someSetting forKey: @"someSetting"];

Upvotes: 3

Sean
Sean

Reputation: 5820

You can include a file in the bundle by adding it to your Resources folder in Xcode and making sure that it is included in your target as well. To access a file in the bundle (e.g., a text file called info.txt), you can get the path using:

[[NSBundle mainBundle] pathForResource:@"info" ofType:@"txt"]

Then access it using the normal NSFileManager methods.

EDIT: With this said, you can't write anything into this file inside the bundle on iOS. Instead, you can copy the file from the bundle to a file system location (say, your app's Application Support folder) on first launch, then access it there elsewhere in your app.

Upvotes: 3

Related Questions