Hi ,
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
In this tutorial, I will be showing you how you can save and retrieve different types of data using the NSUserDefaults object. Saving this way is great for when you want to save small amounts of data such as High Scores, Login Information, and program state.
With the NSUserDefaults class, you can save settings and properties related to application or user data. For example, you could save a profile image set by the user or a default color scheme for the application. The objects will be saved in what is known as the iOS “defaults system”. The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions. This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app!
Saving to the NSUserDefaults is great because it does not require any special database knowledge. So if you don’t want/have an SQLite3 database for your app, this would be the way to go.
How to store/save data in NSUserDefaults :
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// store a NSString
[userDefaults setObject:@"TextToSave" forKey:@"keyToLookupString"];
// store an NSInteger
[userDefaults setInteger:42 forKey:@"integerKey"];
// store a Double
[userDefaults setDouble:3.1415 forKey:@"doubleKey"];
// store a Float
[userDefaults setFloat:1.2345678 forKey:@"floatKey"];
// This is suggested to synch userDefaults
[userDefaults synchronize];
Your stored now.You can retrive it whenever you need.
To Retrieve stored data from NSUserDefaults :
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [userDefaults stringForKey:@"
keyToLookupString
"];
// getting an NSInteger
NSInteger myInt = [userDefaults integerForKey:@"
integerKey
"];
// getting an Float
float myFloat = [userDefaults floatForKey:@"
floatKey
"];
You just retrieved the values stored.
If you have any questions or comments, feel free to leave them in the comments section of this post. Happy iCoding!
wow that post is nice and if u have some code then please share with me i m waiting for yours the next post
ReplyDeletethanks
iPad Application Developer And iPhone 5 App Development