Please explain NSUserDefault purpose - iphone

NSUserDefaults *nsu=[NSUserDefaults standardUserDefaults];
[nsu setObject:[postdata objectForKey:#"url"] forKey:#"url"];
Can anyone explain purpose of NSUserDefault?

I think it is worth mentioning that NSUserDefaults can and should be used to store small amounts of data dealing with user settings and app configuration not set in the Settings.bundle. It is not recommended storing anything with sensitive user information (username, password, etc) as these values are saved in a plain text .plist file in the documents directory.
User defaults can be very flexible and the sky is the limit as to its use, but care should be taken to protect the user's private data. For login information, use the built in keychain. For everything else, there are plenty of options. (e.g. CoreDate, Sqlite)

I hope this is help u.....
NSUserDefaults is a quick and easy way to store small amounts of data for you app. In this example, I’m going to show you how to use NSUserDefaults to store and retrieve data.
Storing Data
// create a standardUserDefaults variable
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
// saving an NSString
[standardUserDefaults setObject:#"mystring" forKey:#"string"];
// saving an NSInteger
[standardUserDefaults setInteger:42 forKey:#"integer"];
// saving a Double
[standardUserDefaults setDouble:3.1415 forKey:#"double"];
// saving a Float
[standardUserDefaults setFloat:3.1415 forKey:#"float"];
// synchronize the settings
[standardUserDefaults synchronize];
Retrieving Data
// create a standardUserDefaults variable
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
// getting an NSString object
NSString *myString = [standardUserDefaults stringForKey:#"keyToLookupString"];
// getting an NSInteger object
NSInteger myInt = [standardUserDefaults integerForKey:#"integerKey"];
// getting an Float object
float myFloat = [standardUserDefaults floatForKey:#"floatKey"];
Try this Example also
http://mobile.tutsplus.com/tutorials/iphone/nsuserdefaults_iphone-sdk/

If you want to store data like user_id, name, etc., for your application, which can be accessible anywhere throughout your application, then NSUserDefaults can be used. It is like storing data in user preferences.
For more details, see
NSUserDefaults

NSUserDefault is generally used for storing data for an app in a global manner within the app. When you store a value in NSUserDefault it will be available until the app is removed from the device. In your above example, you store url in key url. You can access the value from anywhere using:
NSUserDefaults *nsu=[NSUserDefaults standardUserDefaults];
NSString *url = [nsu ObjectforKey:#"url"];

NSUserDefault is used to save small amount of data in your App. If your application needs to store certain data which is small in size, so to better avoid concepts like SQLite we use NSUserDefault. I suggest you to go through the following link for better understanding.
Click here for Tutorial

Related

Use a mysql returned variable in IOS app for otherJSON calls

I have a app and I connect with Json to a mysql DB using PHP and return a value ( UserID ). I now would like to use that value returned for other JSON calls in my WHERE statements.
Do i store the value in a plist file?
How do i make that a global value for that specific user?
Thanks
All you need to do is to save this value in your NSUserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// write value
[defaults setInteger:value forKey:#"value"];
[defaults synchronize];
// read
// if value is not an int but an NSNumber, change it to [defaults objectForKey:#"value"]
value = [defaults intForKey:#"value"];
Historically on iOS, the device UDID has been used as a user's ID in databases. That's recently been banned by Apple, so unless you're forcing users to register with a username and password, you should use the vendor ID as their user ID in the database. The vendor ID is determined using:
NSString* vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
You should pass this value to the server and insert it into your user table as the user's ID.
You can use NSUserDefault to store any variable that is common throughout the application. You can add the object in it and you can remove that when application terminates or that particular User logs out from the application.
e.g.
[[NSUserDefaults standardUserDefaults]setObject:#"10" forKey:#"UserID"];
[[NSUserDefaults standardUserDefaults]synchronize];
And to remove
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"UserID"];

Setting a preference value from stored preference

Playing with my code today,
I run this particular piece of code several times in minor variations throughout a particular class, I'm trying to streamline though. The difference in effect is minimal but changes the amount of code by a volume of hundreds or thousands of lines so would be a big personal win for me.
Essentially I have a value stored as an integer with a key of 'codeKey' and I want to insert the value of that key where the number 30061 currently resides. I'm at a bit of a loss how, can anyone help me out with this one?
I know I need to recall the value somehow and place it in but I'm not really sure how that would look.
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"buttonID"] == 1) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:30061 forKey:#"scifi1"];
[userDefaults synchronize];}
I take it you mean dynamically saving this information without duplicating the same code over and over. If that is correct, your solution will be something like this:
-(void)saveCodeKey:(int)key {
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"buttonID"] == 1) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:key forKey:#"scifi1"];
[userDefaults synchronize];
}
}
Now you can simply invoke [self saveCodeKey:12345]; Assuming the -saveCodeKey: method resides in the same class.
Hope this helps !

Write/Read from property list on iPhone

I know there are thousands of tutorials on the topic however I don't get it to work.
What I am trying to do is to read an value from a plist file. Then I check whether or not I have to update the value. This part is ok. However I don't managed to get the value saved in plist file. I have a Settings.Bundle and it is where I am trying to read/store.
It is just a simple string.
Thanks in advance
The following will give you a NSDictionary with your settings bundle information.
[[NSBundle mainBundle] infoDictionary]
If there are any other generic plist you would like to read you can easily load them into a NSDictionary using
[NSDictionary dictionaryWithContentsOfFile:filePath];
//or
[[NSDictionary alloc] initWithContentsOfFile:filePath]
Also I would like to add for reading and saving values I would recommend doing that in the NSUserDefaults
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
id anObj = [userDefaults objectForKey:#"myKey"];
//Modify anObj
...
[userDefaults setObject:anObj forKey:#"myKey"];
[userDefaults synchronize];//Save immediately if you choose
You cannot modify files/folders present in your app bundle (such as Settings.bundle.) They are read-only.

How to read from app preferences?

I read Apple Programming Guide on this subject but couldn't figure it out.
I created a Settings bundle using the following tutorial, and I tried accessing my preferences (edited manually) like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[self setShouldPlaySounds:[defaults boolForKey:#"play_sounds_preference"]];
for "Key" I used the key value entered in the xml editor (double click on Root.plist).
I know you can build preferences with "Identifier" key and "DefaultValue" but I don't want the settings to be accesible in the setting app, I just want two dictuinaries with some strings for my custom settings.
What am I doing wrong? Why can't I get the value of the preferences?
Is it simpler to create my own config file? Implementing a serializer.
Are you setting the value anywhere? And what behavior are you seeing with respect to the value of shouldPlaySounds?
What happens if you execute this first?
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"play_sounds_preference"];
BOOL result = [defaults synchronize]; // force immediate saving of defaults.
NSLog(#"[defaults synchronize] returned %d", result);

can we make the user know the date on which the data updated from internet with iphone sdk?

i m making an iphone app that displays data from a website on a table view .basically i m atoring the website's data in an array and then display it on a table. now i want whenever the website update their data the user get the date when the data was updated .can anybody tell some different ways to do it?
So, are you storing (caching) the website's data on the phone and only updating once in a while, or are to loading the website and populating the table every time the app starts?
If you are not storing it (with Core Data, User Defaults, NSKeyedArchiver, etc.) it will be difficult for how can you tell that the data has changed?
In that case (not storing) you could after having pulled the data from the website generate the hash value of the data and store it using NSUserDefaults along with a NSDate.
The next time you pull the information from the website you will generate the hash again and compare it to the previous value. If they match, then the content was not updated, else it was and you store the new hash value and the current date.
If you have control over the website you can do a couple of things.
Return a HTTP header with information about when the website was last updated and compare it with a date stored locally. This will allow you to use the HEAD HTTP semantic to check if a full GET request is needed allowing you to minimize network access.
Implement push notifications (does seem over-kill for your situation) telling the user that the content was updated.
Best of luck.
UPDATE
So your best bet would be calling [myArray hash] and store it in NSUserDefaults, e.g.
NSUInteger hashValue = [myArray hash];
NSDate *now = [NSDate now];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:hashValue forKey:#"hash"];
[prefs setObject:now forKey:#"date"]; // Maybe you have to convert the date to a string.
[prefs synchronize];
And later load it when the app starts (or becomes active):
// Load the website again and generate the hash.
NSUInteger newHashValue = [myArray hash];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSUInteger *oldHashValue = [prefs stringForKey:#"hash"];
NSDate *contentDate;
if (newHashValue == oldHashValue)
{
contentDate = [prefs dateForKey:#"date"];
}
else
{
contentDate = [NSDate now];
// Store the new date.
// and the new hash.
}