Setting Up a Plist to Store Application Data (Not Settings) for an iPhone Game

Setting up a plist to store application data (not settings) for an iPhone game

  • Add your plist file as a resource file in your project. Say, it's name is config.plist

  • Get the path for the resource file. (Though, be careful of [NSBundle mainBundle] as it will not return the unit test bundle in a unit test.)


NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
  • Your root object is an array of levels. Load it in a NSArray.

// this is autoreleased. you can retain it if needed
NSArray *levelArray = [NSArray arrayWithContentsOfFile:plistPath];
  • Now you have the array of levels. Each level is a dictionary. Load the level i (counting from 0).

NSDictionary *level = [levelArray objectAtIndex:i];
  • Now you can get the objects from level dictionary by using objectForKey method. For example, to get the sequence array:

NSArray *seq = [level objectForKey:@"levelSequence"];
  • You can loop through the levelArray to alloc, init and add to levels array for all your levels.

Hope it helps. Please note that I have not compiled the code, so there might be some typos.

How to include large set of data with my iOS app at install?

If you have a dictionary, use writeToFile:atomically: to save it to a file and initWithContentsOfFile: to load it back in.

That said, if it's a lot of data, and particularly if you don't need all of it loaded all the time, consider saving a Core Data store and deploying that.

How to restrict the app to iphone only and preserve this setting

Something that you could try is to set up the UIDeviceFamily in your project Info.plist. For this key, you could set 1 for iPhones or 2 for iPads. For example:

<key>UIDeviceFamily</key>
<array>
<integer>2</integer>
</array>

Updating and saving data in plist

The easiest way to create a dictionary from a plist is to use the method dictionaryWithContentsOfFile:,

For example, to load a plist from your resources:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"playerData" ofType:@"plist"];
NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

Writing a plistdict is equally simple:

[plistdict writeToFile:filePath atomically:YES];

Note that you can't write into your app's resources, so you'll have to create a different path, e.g. in your Documents directory for your plist.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerData.plist"];
[plistdict writeToFile:docfilePath atomically:YES];

Now retrieve data from plist again.

 NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];

Add your content in plistDict and write it again.

How to use pList in iOS Programming

The URL based method for this:

// Get the URL for the document directory
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *documentDirectoryURL = [[fileManager URLsForDocumentDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];

// Turn the filename into a string safe for use in a URL
NSString *safeString = [@"scores.plist" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Create an array for the score
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:score]];

// Write this array to a URL
NSURL *arrayURL = [NSURL URLWithString:safeString relativeToURL:documentDirectoryURL];
[array writeToURL:arrayURL atomically:YES];

Download .plist from server and save it locally in iPhone application

First: Why don't you use Apple's services for In-App purchases? Why using your very own implementation? I am not sure but...do you think you will get Apple's approval? As far as I remember, Amazon did something similar in the past and they didn't get it.

Second: I wouldn't use NSData for this purpose. If you have a valid property list, then it is the best solution to use

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"foobar.com/plistfile.plist"]];

You could then store this dictionary. To be honest, I wouldn't recommend using your solution. If I understood you correctly, you would have thousands of plists after some time. My suggestion: Use a "global" mutable dictionary as ivar of your App Delegate for example. This should be empty at the beginning. Then download your helper dictionaries as mentioned above and populate your global dictionary with

[[delegateInstance globalDictionary] addEntriesFromDictionary:dict];

each time you get new data.

Save it with

[[delegateInstance globalDictionary] writeToFile:@"path" atomically:YES];

and recover it each time your app starts up.

EDIT:

Of course you can do it that way. If these plists really store information, that's fine. For example you could store billing information there, so that they are not interchangeable for other users (or shouldn't be at least). BUT: If you only want to get information about the question if a users bought a specific level or not consider combining your information within one plist (as described above). Anyway: Consider a possibility for your users to get back their data. Think of somebody deleting your app accidentally. How do you want to explain to him all his purchases are lost? In-App purchases are safer.

Or at least: Store a copy of the receipts on your server and let the users login to gather what they paid for in the past ;-)

Have fun and good luck! :-)



Related Topics



Leave a reply



Submit