iPhone equivalent of Android's SavedPreferences - iphone

I'm writing an app for the iPhone that I've already created in Android and Blackberry and was hoping that there was an equivalent for SavedPreferences in iOS. Specifically I'm looking for the functionality to be able to create favorites on the fly. So in Android I can save a string into SavedPreferences with the identifier of "server project favorites". Is there something similar I can do in iOS without bothering with a SQL database for simple strings?

I think NSUserDefaults is what you are looking for.
Example with an array for server project favorites:
// Saving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *favorites = [NSArray arrayWithObjects:#"Favorite 1", #"Favorite 2", nil];
[defaults setObject:favorites forKey:#"favorites"];
// Retrieving
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *favorites = [defaults arrayForKey:#"favorites"];

You're looking for NSUserDefaults

Related

Retrieval of NSArray from NSUserDefaults returns empty array

I am experiencing strange behaviour with NSUserDefaults. I am initially storing an array to the user defaults in my AppDelegate.m:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:#"weekdayIDs"];
if (weekdayIDs == nil) {
weekdayIDs = [NSArray arrayWithObjects:#"su", #"mo", #"tu", #"we", #"th", #"fr", #"sa", nil];
[defaults setObject:weekdayIDs forKey:#"weekdayIDs"];
}
[defaults synchronize];
Now in a different view controller ContentViewController.m, I want to retrieve the array:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *weekdayIDs = [defaults objectForKey:#"weekdayIDs"];
But I just get an array without objects, although its count == 7. I also used arrayForKey: but with the same result. I added a screenshot from my breakpoint.
I am regularly using NSUserDefaults, but currently I am bit stuck on this. It's probably a stupid mistake, anyone care to help?
Thank you so much!
-- Update:
I also figured it might be a problem with the init of the NSArray in the first place, but even replacing its objects with manually created NSString *dwid_su = [NSString stringWithString:#"su"]; didn't work.
Your code works perfectly.
Just, print the description of you array and you will see what you want.
Right click on weekdayIDs variable and select Print Description of weekdayIDs
or use through lldb debugger console po weekdayIDs
or NSLog(#"%#", weekdayIDs);
Here the results.

NSUserDefault memory management issue

I'm saving my class instance with some data (images, strings, arrays etc) inside to my NSUserDefaults using this code:
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:myClassInstance];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedData forKey:#"myClassData"];
[defaults synchronize];
I'm doing this to use this data if I won't have internet connection.
Does all this data load to memory with NSuserDefaults? Or it loads when i use this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:#"myClassData"];
It will load when you call
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:#"myClassData"];
I am not sure when the data is loaded, but according to guidelines by Apple, you should not use nsuserdefaults to store any amount of data, as it is slow. You should just write the data as a file. NSDictionary and NSArray can write them self to file and read back directly.
You really should not do this. NSUserdefaults is only thought for doing preferences and settings, very small stuff

How to keep my data in a array-iphone sdk

I'm trying to create an application, and in that I'm receiving some contents from net and loading into and array, if I quit and run the app again i'm losing the data. How can I store the data in the app itself. Should I use some kind of database? If so which one and how can I implement that? I need to store only some string variables.
EDIT:
My app is tabbar based app, and have two tabs. Loading the arrays in one tab and loading that array in a table in the other VC. once I load the array and move to the second tab, the table is loaded with the array. If i go back and add few more values and then if i check, the added datas is not displayed in the table. And also, when I quit the app the table lose all the values, I need to keep the values in the table, even after i quit the app. How can I do that?
here is my code:
in viewdidload:
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:alertList forKey:#"myArray"];
[simplePlistDb synchronize];
in cellforrowatindexpath:-
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray){
NSLog(#"Str:%#", str);
[loadArray addObject:str];
}
cell.textLabel.text = [loadArray objectAtIndex:indexPath.row];
}
Thank you.
You will want to look into Core Data if you want to save a decent amount of data to the iPhone. However, if not, you can just load the items in the array into a plist and load them from there at launch.
Plist Writing: How to create a new custom property list in iPhone Applications
CoreData: http://www.raywenderlich.com/934/core-data-tutorial-getting-started
Edit--->
As Nekto said, you can also use NSUserDefaults, but I advise mainly using NSUserDefaults for simple NSStrings and integers.
I think for your purposes NSUserDefaults will be enough : NSUserDefaults Class Reference.
Examples:
// save
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
[simplePlistDb setBool:YES forKey:#"isItWorking"];
[simplePlistDb setObject:[NSArray arrayWithObjects:#"very", #"cool", nil]];
[simplePlistDb synchronize];
// restore
NSUserDefaults *simplePlistDb = [NSUserDefaults standardUserDefaults];
BOOL flag = [simplePlistDb boolForKey:#"isItWorking"];
if (flag)
{
NSArray *myArray = [simplePlistDb objectForKey:#"myArray"];
for (NSString *str in myArray)
NSLog(#"%#", str);
}
You can use NSUserDefaults in order to store your data till the time your app is installed in your device.
How to write in NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"yourObject1" forKey:#"correspopndingKey1"];
[defaults setObject:#"yourObject2" forKey:#"correspopndingKey2"];
[defaults synchronize];
How to read from NSuserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *temp1 = [defaults objectForKey:#"correspopndingKey1"];
NSString *temp2 = [defaults objectForKey:#"correspopndingKey2"];
You can store NSArray in a similar way.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:objArray forKey:#"correspopndingKey"];
[defaults synchronize];

XCode Cache Tutorial

Hey, I am trying to cache info on my IPhone App, but don't even know where to get started, I would to just cache a string (ex: 123123-12313123-1231231-123123), so when the user comes back it will remember it.
Is there a tutorial I can follow some where or could someone provide me some code?
Thanks
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setString:#"123123123" forKey:#"myKey"];
[userDefaults synchronize];
Later, when you wish to retrieve this object:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *myString = [userDefaults stringForKey:#"myKey"];
You'll, of course, want to manage the keys properly, etc.

iPhone Xcode Settings.bundle Plist Help

I followed the tutorial: http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
And the "Shuffle Switch" (that I just created based on the tutorial) was not in the Settings App. Every time I did an NSLog on the state of the switch, it would return "(null)". The "Slideshow Switch" (which was created the same way) worked fine.
My Settings bundle Root.plist file looks as follows: (copy link and paste into web browser)
i.imgur.com/kb8DT.png
Please help as I need to create, and access a Toggle Switch created in the .plist file. I am new to iPhone Programming.
Here's the code I'm using to set the user preference switch:
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES" forKey:#"ShuffleToggleKey"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
And here's the code I'm using to get the state of the user preference switch:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];
It seems, that you're putting string object and trying to get boolean value.
You should or get out the string like
NSString *enabledStr = [defaults stringForKey:#"ShuffleToggleKey"];
BOOL enabled = [enabledStr boolValue];
or put a boolean value in the first place like that:
[defaults setBool:YES forKey:#"ShuffleToggleKey"];
Then you can retrieve it as
BOOL enabled = [defaults boolForKey:#"ShuffleToggleKey"];