~If you would like to request a tutorial, email Collateral at collateraldamag3@gmail.com~

Share This!

iPhone Tutorial ~ NSUserDefaults and Settings.bundle - Part II

in





Ok, now that we have learned the basics of NSUserDefaults, we can now start to make a Settings Bundle. What is a Settings Bundle you may ask? A settings bundle is the application preferences that you see in the Settings.app. These are loaded automatically when you enter Settings.app and allow you to change the NSUserDefaults from outside the application itself; a very helpful feature.


To make a Settings Bundle we have to start off by making a folder named "Settings". Inside of that, make a new file in TextEdit and make it plain text. 






Then fill your plain-text document like this:



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
   Root.plist
   Preference settings for MyApp
-->
<plist version="1.0">
    <dict>
    </dict>
</plist>
Once you are done, save it as "Root.plist". Now rename your "Settings" folder to "Settings.bundle". The settings folder should become a lego-block-like icon and the Root.plist should not be visible. Now, Open Xcode and open your project. Click "Project" and then "Add to Project".





Then choose your Settings.bundle. Click the arrow next to it in Xcode and you should see your Root.plist again. If you can, good, move on. If not, check to make sure everything is spelled correctly. "Settings.bundle" and "Root.plist" are CASE SENSITIVE.

Now as an example, we will add a username to the preferences. So open up Root.plist in the Property List Editor (Right Click and Select "Open with Finder"). You should see a key labeled "Root", click on it and then click "Add Child." Make the new child's key "Title", make its type "String", and make its value the name of your application. Now click on Root again. Make another child, this time with a key named "PreferenceSpecifiers". Make its type "Array".

Now click on "PreferenceSpecifiers" and then on "Add Child" (You should now be adding a child to PreferenceSpecifiers, not to Root). This items key, by default, should be "Item 0". Make its type "Dictionary".

Almost done. Now add 4 children to "Item 0" and make them match what I have below:



The "Title" key for Item 0 is the title that the user will see when he/she edits this in Settings.app. The "Type" key tells Settings.app that it is a PSTextFieldSpecifier, which is basically a UITextField. The "Key" key tells the program which key we are loading from in our code. NOTE: THIS IS OUR VARIABLE NAME FOR INSIDE THE CODE. See Here. And the default value is pretty self-explainatory. It is the value that is loaded if none is entered.

Now save Root.plist. Compile and run your app in the simulator. Exit the app and enter Settings.app. Check to see if there is an entry for your app at the bottom. If there is, then you have done it correctly.

For this example, the code to load the username we stored into a NSString would be:








NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *strUserName = [prefs stringForKey:@"userName"];
Here are the other types for storing NSUserDefaults into your Settings.bundle:






  1. PSTextFieldSpecifier
  2. PSTitleValueSpecifier
  3. PSToggleSwitchSpecifier
  4. PSSliderSpecifier
  5. PSMultiValueSpecifier
  6. PSGroupSpecifier
  7. PSChildPaneSpecifier
Congratulations! You now have a working Settings Bundle and you can store and load information from NSUserDefaults!

Go Back to Part I

~Collateral

No comments:

Post a Comment

This entry is filed under .

You can also follow any responses to all entry through the RSS Comments feed.