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

Share This!

Showing posts with label NSUserDefaults. Show all posts
Showing posts with label NSUserDefaults. Show all posts

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

Read More...

iPhone Tutorial ~ NSUserDefaults and Settings.bundle - Part I

in


Alright, This is a tutorial on the NSUserDefaults feature and how to use them in conjunction with Settings.bundle.


First off, What is NSUserDefaults? If you want the "Apple Definition" you can find it HERE. But basically, NSUserDefaults is a way to store data for a another point in time. You can use NSUserDefaults to save a character's level, or what items he/she's carrying. Or You can use it to remember a username and password or whether the user prefers a light or dark GUI. Basically, it saves stuff. 
So how do I use NSUserDefaults? Well, Apple has made it VERY easy actually. There are two ways to access data in NSUserDefaults, saving data and loading data. We'll start with saving.





NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: 0 forKey: @"whatever"];//Save
[prefs synchronize];


 That ^^ is a very simple example of saving data to NSUserDefaults. I'm starting off by labeling [NSUserDefaults standardUserDefaults] as just simply "prefs", mostly because prefs is about 20x easier to write.  I am then saving "0" as an integer to the key "whatever". Now, you may be asking, what is a key? A key is basically a save slot. whenever you have an option you have a slot. For example, if you have a game where you have to save the character's inventory, you will probably store an array of the character's objects into a key named "inventory". Basically a key is a spot in the NSUserDefaults where certain info is stored.  And [prefs synchronize] just writes it to the disk.


Here are the different syntaxes you can use when saving to NSUserDefaults (of course all linked to their reference :p):





NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *strUserName = [prefs stringForKey:@"userName"];




Here I am loading a String from the key "userName" and storing it into a NSString titled "strUserName". Easy Peasy. Here are the syntax's and references for what you can use while loading data. Be careful it IS different from the first list.






Read More...