2011年10月18日星期二

Using a plist to Package Content with Your App

Using a plist to Package Content with Your App:
Most apps come packaged with content like images, videos and music and the process of including assets like this in your app is pretty straightforward. You simply drag these assets into your XCode project and check the box that says “create copy”. But, what about more structured data like lists, recipes and content that you would generally expect to be located in a database?

iOS SDK Data Storage Solutions


Your gut reaction may be to add Core Data or set up a SQLite database. Both of these solutions work great when you want your app to remember user entered content so it may seem logical to simply include a database with your content that you can access through these data persistance solutions.

Core Data & SQLite Are Terrible Solutions


I don’t mean that these are terrible solutions for persisting user generated data. However, there is no great way to include a pre-populated Core Data store with your app without some painful hacking. The standard practice is to populate the Core Data store on the first run of your app (yuck).

Including a SQLite database is not much better in terms of including content. While you can certainly include a pre-populated SQLite database in with your app you’ll need a SQLite utility to help with this or an expertise in the SQL database language. This is really too much work for most applications.

Property Lists (plists) Will Save The Day


Seasoned Mac and iOS developers are probably saying duh right about now because the super-useful plist is exactly what you need for this situation. If you never heard about plists before don’t feel so bad, it’s not all that obvious that plists are available at first glance. In fact, I myself didn’t bother looking into plists until Rick, Code Camper and creator of ColdCalcPro, pressed me on using these for another app he’s working on that requires storing lists of values.

Plist Tutorial


Before we get into the step by step procedure on using plists let’s talk a little bit about what plists are. A plist, or property list, is a list of content that is referenced by keys. If you were to open a plist file in a text editor, you would see that the file is a XML document where each piece of content is enclosed by tags (like web page). Here is an example of what a plist would look like if you were to open it up in a text editor:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>punchbowl</key>
<string>Punch Bowl</string>
<key>cds</key>
<array>
<string>U2</string>
<string>Bjork</string>
</array>
<key>invitations</key>
<array>
<string>Eileen</string>
<string>Harry</string>
<string>June</string>
<string>Tom</string>
</array>
</dict>
</plist>

plists work great for these small bits of data that you want to include in your app because they can be loaded into Objective-C objects based on NSMutableDictionary which make them easy to use.

So I’m going to work through an example of using a plist to bring content into my app. The content is going to be what you see above: a list of stuff you might want for a party. This was you will see how to store simple strings in a plist as well as an array so you can have more complex data structures.

Adding plists to iOS Apps


You add a plist like other files in XCode. First you go the XCode menu and choose File > New File.

In the dialog box that pops up you choose iOS > Resource > Property List


Your plist will be called something like whatever.plist. Click on that file to open up the plist editor.

Adding plist Content


To add content all you need to do is control-click on the editor window in the middle and select Add Row.



Three columns will appear: Key, Type & Value. You can edit all three of these. The Key is a string that you will use in your XCode project to retrieve the information contained in the Value field. You can specify the data type in the Type field.



Here is a simple example where I set the Key to keyToContent01, the Type to String and the Value to Hello World!.



Here is what my plist called partysupplies.plist looks like:



Getting Your Content In Your Code


plist files will get put into your app’s bundle and so you will need to access your plists by first getting a reference to your user’s bundle directory and then using a special NSMutableDictionary constructor to create a dictionary based on the contents of the plist file.

Here’s how:

NSString *mainBundle = [[NSBundle mainBundle] resourcePath];

NSString *fileName = [NSString stringWithFormat:@"%@/partysupplies.plist",
mainBundle];

NSMutableDictionary *partySupplies = [[NSMutableDictionary alloc]
initWithContentsOfFile:fileName];

To get the punch bowl text you would send the NSDictionary message objectForKey using the key from the plist file (here punchbowl).

NSString *punchBowlName = [partySupplies objectForKey:@"punchbowl"];

NSLog(@"punchBowlName=%@", punchBowlName);

You can do something similar to get the list of invitations for the party:

NSArray *listOfInvitations = [partySupplies objectForKey:@"invitations"];

for(NSString *invitation in listOfInvitations)
NSLog(@"%@ is invited", invitation);

Questions?


That’s how to use plists – try this out on your own. Post any questions that you may have below.

没有评论:

发表评论